簡體   English   中英

如何在球衣2和HK2中使用自定義DI

[英]How to use custom DI with jersey 2 and hk2

我只是想在這里遵循有關依賴項注入的jersey文檔: https : //jersey.java.net/documentation/latest/ioc.html#d0e15100

如果嘗試在參數上使用@Inject,我只會得到grizzly的請求失敗頁面。

有人可以告訴我我在做什么錯嗎?

Main.java

public class Main extends ResourceConfig {
    // Base URI the Grizzly HTTP server will listen on
    public static final String BASE_URI = "http://0.0.0.0";
    public static URI getBaseURI(String hostname, int port) {
        return UriBuilder.fromUri("http://0.0.0.0/").port(port).build();
    }

    public Main() {
        super();

        String port = System.getenv("PORT");
        if(port == null) {
            port = "8080";
        }
        URI uri = getBaseURI(System.getenv("HOSTNAME"), Integer.parseInt(port));
        final HttpServer server = startServer(uri);
        System.out.println(String.format("Jersey app started with WADL available at "
                + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));

        register(new AbstractBinder() {
            @Override
            protected void configure() {
                bindFactory(DaoFactory.class).to(TodoDao.class);
            }
        });

        try {
            while(true) {
                System.in.read();
            }
        } catch (Exception e) {

        }
    }

    /**
     * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
     * @return Grizzly HTTP server.
     */
    public static HttpServer startServer(URI uri) {

        final ResourceConfig rc = new ResourceConfig().packages("com.example");

        return GrizzlyHttpServerFactory.createHttpServer(uri, rc);
    }

    /**
     * Main method.
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        Main m = new Main();

    }
}

TodoResource.java

@Path( "todos" )
public class TodoResource {

    @Inject Dao<String> dao;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        if(dao == null) {
            return "dao is null";
        }
        StringBuilder builder = new StringBuilder();
        for(int i = 0; i < dao.getAll().size(); i++) {
            builder.append(dao.getAll().get(i));
        }
        return builder.toString();
    }

}

DaoFactory.java

public class DaoFactory implements Factory<Dao>{

    private final Dao dao;


    @Inject
    public DaoFactory(Dao dao) {
        this.dao = dao;
    }

    @Override
    public Dao provide() {
        return dao;
    }

    @Override
    public void dispose(Dao d) {

    }
}

您對綁定所做的操作的格式正確。

但是,您將綁定到帶@Contract注釋的接口“ TodoDao”,這意味着它將查找接口“ TodoDao”進行注入。 在您的類TodoResource中,您有“ Dao <String>”,這將不匹配。 因此,如果將其換成TodoDao,它應該找到並替換它。

現在,如果要使用泛型而不是具體的類,則必須使用帶有包裝器的實例化對象。 形式的東西

bind(x.class).to(new TypeLiteral<InjectionResolver<SessionInject>>(){});

另外,如果您需要自動綁定的幫助(類似於Spring,則可以),可以使用以下文章: http ://www.justinleegrant.com/?p=516

暫無
暫無

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

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