Nam*_*man 14 java maven java-8 java-9 java-module
在将我们的一个项目迁移到Java 9(build 9 + 181)时,我遇到了一个特殊的问题,在类型推断和java模块相关的某些库中看起来像是一个不正确的实现.我使用的是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));
}
}
Run Code Online (Sandbox Code Playgroud)
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);
..
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下组合,配置对我来说很好:
但是当我切换到模块结构然后尝试编译由这些类组成的maven模块时,我在以下期间遇到这些错误mvn clean install:
Run Code Online (Sandbox Code Playgroud)[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 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;
}
Run Code Online (Sandbox Code Playgroud)
Q2.如果这不是迁移时的回归问题,为什么这不会因我们之前的设置而失败?这是否也要求我们的服务中的代码更改,或者我们是否应该等待库移动到模块,如果这可能有帮助?
注意:尝试研究依赖版本和类实现的用法.它们在先前和当前设置中都相同.
如果我能提供任何进一步的信息,请告诉我.
更新:我能够在我创建的微服务示例中重现它,以将其与我项目的其余部分隔离开来.
Nik*_*kar 12
从示例项目中我能够解决编译问题.方法中有2个例外com.SomeService#run.您的module-info.java中缺少模块,一旦添加这些模块,代码就应该编译.
requires dropwizard.jersey;
requires dropwizard.jetty;
Run Code Online (Sandbox Code Playgroud)
JerseyEnvironment 来自 io.dropwizard:dropwizard-jersey:1.1.0
ServletEnvironment 来自 io.dropwizard:dropwizard-jetty:1.1.0
由于它们是不同的罐子,因此它们会输出不同的模块.因此,需要明确添加要求.没有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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
898 次 |
| 最近记录: |