簡體   English   中英

Vertx&HK2-啟動應用程序(依賴注入)

[英]Vertx & HK2 - Launching the application (dependency injection)

我正在嘗試構建具有Vert.x和HK2擴展名的應用程序以進行依賴項注入。 但是,我似乎找不到任何可以向我展示整個畫面的示例。

請注意,我是依賴注入的新手。

我做了如本示例所示的操作,但是在啟動應用程序時遇到了NoSuchMethodException,因為它試圖訪問verticle類(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;
    }

}

在我看來,您實際上需要在某個地方創建ServiceLocator。 像這樣:

private void startServiceLocator() {
  ServiceLocatorUtilities.bind("MyServiceLocator", new ConfigBinder());
}

有關更多信息,請參見bind

另外,有關如何啟動hk2的更多信息: 入門

設法修復它。

我切換到Guice,我為ConfigReaderImpl類的JsonObject注入丟失了一些東西

新的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM