2017-10-16 235 views
0

我试图用Vert.x和HK2扩展为依赖项注入构建应用程序。但是,我似乎无法找到任何能够让我看到完整画面的例子。Vertx&HK2 - 启动应用程序(依赖项注入)

请注意,我完全不了解依赖注入。

我按照this example所示的方法做了,但是在启动应用程序时,我得到一个NoSuchMethodException,因为它试图访问vertex类(SimpleVerticle)中不存在的默认无参数构造函数。

在我的build.gradle中,mainClassName设置为'io.vertx.core.Launcher',并且在shadowJar清单属性中,“Main-Verticle”设置为SimpleVerticle,如示例中所示。

当然,我失去了某个地方的东西。任何人都可以告诉我我错过了什么,或者指给我一些最新的完整示例?

  • Vert.x版本:3.4.2
  • Vert.x HK2版本:2.5.0

的build.gradle:

buildscript { 
    repositories { 
     mavenCentral() 
    } 
} 

plugins { 
    id 'java' 
    id 'application' 
    id 'com.github.johnrengelman.shadow' version '1.2.3' 
    id 'java-library' 
    id 'eclipse' 
    id 'idea' 
    id 'maven' 
    id 'groovy' 
    id 'jacoco' 
} 


group 'example' 
version "${buildVersion}" 

repositories { 
    mavenCentral() 
} 

sourceCompatibility = '1.8' 
targetCompatibility = '1.8' 

dependencies { 
    compile('io.vertx:vertx-core:3.4.2') 
    compile('io.vertx:vertx-web:3.4.2') 
    compile('javax.json:javax.json-api:1.1') 
    compile('org.glassfish:javax.json:1.0.4') 
    compile('log4j:log4j:1.2.17') 
    compile('io.vertx:vertx-web-client:3.4.2') 
    compile('com.englishtown.vertx:vertx-hk2:2.5.0') 
    testCompile "junit:junit:4.12" 
    testCompile "io.vertx:vertx-unit:3.3.3" 
    testCompile "com.jayway.restassured:rest-assured:2.4.0" 
    testImplementation 'junit:junit:4.12' 
} 

mainClassName = 'io.vertx.core.Launcher' 

shadowJar { 
    classifier = 'fat' 
    baseName = 'aggregator-api' 
    manifest { 
     attributes "Main-Verticle": 'example.startup.StartupVerticle' 
    } 
    mergeServiceFiles { 
     include 'META-INF/services/io.vertx.core.spi.VerticleFactory' 
    } 
} 

StartupVerticle:

package example.startup; 

import example.config.ConfigReader; 
import io.vertx.core.AbstractVerticle; 

import javax.inject.Inject; 

public class StartupVerticle extends AbstractVerticle { 

    private final ConfigReader configReader; 

    @Inject 
    public StartupVerticle(final ConfigReader configReader) { 
     this.configReader = configReader; 
    } 

    @Override 
    public void start() throws Exception { 
     if(this.configReader == null) throw new IllegalStateException("ConfigReader was not injected!"); 

     super.start(); 
     System.out.println("Started verticle using DI"); 
    } 

} 

ConfigBinder:

package example.binder; 

import example.config.ConfigReader; 
import example.config.ConfigReaderImpl; 
import org.glassfish.hk2.utilities.binding.AbstractBinder; 

public class ConfigBinder extends AbstractBinder { 

    @Override 
    protected void configure() { 
     this.bind(ConfigReaderImpl.class).to(ConfigReader.class); 
    } 

} 

ConfigReader:

package example.config; 

import io.vertx.core.json.JsonObject; 

public interface ConfigReader { 

    JsonObject getConfig(); 

} 

ConfigReaderImpl:

package example.config; 

import io.vertx.core.json.JsonObject; 

public class ConfigReaderImpl implements ConfigReader { 

    private final JsonObject config; 

    ConfigReaderImpl(JsonObject config) { 
     this.config = config; 
    } 

    @Override 
    public JsonObject getConfig() { 
     return this.config; 
    } 

} 
+0

如果你没有提供你已经尝试过的东西,怎么能指出失踪的一块? – tmarwen

+0

对不起,我想我已经解释过这种情况了。添加了当前的代码。它基本上是我链接的GitHub示例的副本。 –

+0

感谢您的更新,请务必删除任何公司/组织提及(主要通过包名称):) – tmarwen

回答

0

设法解决它。

我切换到吉斯我错过了一些东西,我的JSONObject注射的ConfigReaderImpl类

新ConfigBinder:

public class ConfigBinder extends AbstractModule { 

    private final String CONFIG_PATH = "config"; 

    @Override 
    protected void configure() { 
     this.bind(ConfigReader.class).to(ConfigReaderImpl.class).in(Scopes.NO_SCOPE); 
     this.bindConfig(Vertx.currentContext().config(), this.CONFIG_PATH); 
    } 

    private void bindConfig(JsonObject config, String path) { 
     this.bind(JsonObject.class).annotatedWith(Names.named(path)).toInstance(config); 
    } 

} 

我还缺少在ConfigReaderImpl类注释:

public class ConfigReaderImpl implements ConfigReader { 

    private final JsonObject config; 

    @Inject 
    private ConfigReaderImpl(@Named("config") final JsonObject config) { 
     this.config = config; 
    } 

    @Override 
    public JsonObject getConfig() { 
     return this.config; 
    } 

} 

而我设法部署一个依赖注入Verticle:

Injector injector = Guice.createInjector(new ConfigBinder()); 
Verticle verticle = injector.getInstance(SomeVerticle.class); 
this.getVertx().deployVerticle(verticle); 
0

在我看来像你需要实际的地方创建了服务定位。事情是这样的:

欲了解更多信息,请参阅bind

而且关于如何启动HK2更多信息:Getting Started