在将我们的项目之一迁移到 Java 9(内部版本9 + 181)时 ,我遇到了一个特殊的问题,那就是在某些与类型推断和java- module相关的库中,这种实现看起来不正确。我正在使用 dropwizard-core(1.1.0) 和 guice(4.1.0) 配置,如下所示:
dropwizard-core(1.1.0)
guice(4.1.0)
public class CustomService extends io.dropwizard.Application<CustomServiceConfig> { public static void main(String[] args) throws Exception { new CustomService().run(args); } // other initializations @Override public void run(CustomServiceConfig config, io.dropwizard.setup.Environment environment) throws Exception { com.google.inject.Injector injector = createInjector(config, environment); environment.jersey().register(injector.getInstance(SomeResource.class)); //line 45 environment.healthChecks().register("DBHealth", injector.getInstance(HealthCheck.class)); environment.servlets().addFilter("Filter-Name", SomeFilter.class) .addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*"); } private com.google.inject.Injector createInjector(CustomServiceConfig config, Environment environment) { return com.google.inject.Guice.createInjector(new CustomServiceModule(config, environment)); } }
public class CustomServiceModule extends com.google.inject.AbstractModule { private final CustomServiceConfig serviceConfig; private final Environment environment; public CustomServiceModule(CustomServiceConfig serviceConfig, Environment environment) { this.serviceConfig = serviceConfig; this.environment = environment; } @Override protected void configure() { bind(SomeInterface.class).to(SomeInterfaceImpl.class); .. } }
通过以下组合,该配置对我而言运行良好:
但是,当我切换到模块结构,然后尝试编译由这些类组成的Maven模块时,在以下过程中出现以下错误 mvn clean install :
mvn clean install
[ERROR] ../service/service/CustomService.java:[45,29] incompatible types: inference variable T has incompatible bounds equality constraints: base.SomeResource upper bounds: java.lang.Class<?>,java.lang.Object [ERROR] ../service/service/CustomService.java:[56,31] no suitable method found for addFilter(java.lang.String,java.lang.Class< SomeFilter >) [ERROR] method io.dropwizard.jetty.setup.ServletEnvironment.addFilter(java.lang.String,javax.servlet.Filter) is not applicable [ERROR] (argument mismatch; java.lang.Class< SomeFilter > cannot be converted to javax.servlet.Filter)
[ERROR] ../service/service/CustomService.java:[45,29] incompatible
types: inference variable T has incompatible bounds equality constraints: base.SomeResource upper bounds: java.lang.Class<?>,java.lang.Object
[ERROR] ../service/service/CustomService.java:[56,31] no suitable method found for addFilter(java.lang.String,java.lang.Class< SomeFilter >) [ERROR] method
io.dropwizard.jetty.setup.ServletEnvironment.addFilter(java.lang.String,javax.servlet.Filter) is not applicable [ERROR] (argument mismatch; java.lang.Class< SomeFilter > cannot be converted to javax.servlet.Filter)
我不确定为什么会发生这些错误,以及是否可能与使用中的模块结构有关。
Q1。 考虑到所使用的依赖项,是否有任何与类型推断相关的更改会影响模块结构更改(如果可能)的Maven编译?
同样在迁移时,我主要使用IntelliJ建议的自动模块名称来构造module-infoas:
module-info
module service { // Internal modules which compile successfully requires model; requires util; // Dependent library modules requires httpcore; requires guice; requires guava; requires dropwizard.core; requires mongo.java.driver; requires org.apache.commons.lang3; requires javax.servlet.api; }
Q2。 如果在迁移时这不是回归问题,为什么在我们之前的设置中没有失败?这是否还会在我们的服务中强制进行代码更改,或者如果有帮助,我们是否应该等待库移至模块?
注意 :已尝试研究依赖项版本和类实现的用法。它们在先前和当前设置中都是相同的。
让我知道我可以提供的任何其他信息。
更新 :我能够在我创建的微服务样本中复制相同内容,以将其与项目的其余部分隔离。
通过示例项目,我能够解决编译问题。方法中有2个例外com.SomeService#run。您的module- info.java中缺少模块,一旦添加这些模块,代码便会编译。
com.SomeService#run
requires dropwizard.jersey; requires dropwizard.jetty;
JerseyEnvironment 来自 io.dropwizard:dropwizard-jersey:1.1.0
JerseyEnvironment
io.dropwizard:dropwizard-jersey:1.1.0
ServletEnvironment 来自 io.dropwizard:dropwizard-jetty:1.1.0
ServletEnvironment
io.dropwizard:dropwizard-jetty:1.1.0
由于它们是不同的jar,因此它们导出不同的模块。因此,需要明确添加需求。您的代码在没有module- info.java的情况下可以正常工作,因为那时不使用模块系统。
我通过执行以下注释中提到的方法找到了解决方法:
@Override public void run(SomeServiceConfig config, Environment environment) throws Exception { Injector injector = Guice.createInjector(new SomeServiceModule()); // Fix: Extract to variable to find Type of jersey and then find the module to add under requires JerseyEnvironment jersey = environment.jersey(); jersey.register(injector.getInstance(SomeResource.class)); // Fix 2: Same method as Fix 1 ServletEnvironment servlets = environment.servlets(); servlets.addFilter("Some-Filter", SomeFilter.class); }