14

在将我们的一个项目迁移到Java 9(build 9+181)时,我遇到了一个特殊的问题,在某些与类型推断相关的使用中的库中实现看起来不正确。我正在使用一个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);
        ..
    }
}

使用以下组合,配置对我来说很好:

  • Java 8 + Maven 3 + Compiler Plugin 3.6.1 [我们的原始设置]
  • Java 9 + Maven 3 + Compiler Plugin 3.7.0(命令行和 Maven 配置更新)

但是当我切换到模块结构然后尝试编译由这些类组成的 maven 模块时,我在以下过程中遇到了这些错误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)

我不确定为什么会发生这些错误,以及它们是否与使用的模块结构有关。

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;
}

Q2。如果这不是迁移时的回归问题,为什么我们之前的设置没有失败?这是否还要求我们更改服务中的代码,或者如果这可能有帮助,我们是否应该等待库迁移到模块?

注意:已尝试查看依赖版本和类实现的使用情况。它们在以前的设置和当前设置中是相同的。

请让我知道我可以提供帮助的任何进一步信息。


更新:我能够在我创建的微服务示例中重现相同的内容,以将其与我的项目的其余部分隔离开来。

4

1 回答 1

12

从示例项目中,我能够解决编译问题。方法中有 2 个异常com.SomeService#run。您的 module-info.java 中缺少模块,添加这些模块后,代码应该可以编译。

requires dropwizard.jersey;
requires dropwizard.jetty;

JerseyEnvironment来自io.dropwizard:dropwizard-jersey: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);
}
于 2017-09-25T02:09:07.067 回答