简体   繁体   English

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

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

I'm just trying to follow along with the jersey docs on dependency injection here: https://jersey.java.net/documentation/latest/ioc.html#d0e15100 我只是想在这里遵循有关依赖项注入的jersey文档: https : //jersey.java.net/documentation/latest/ioc.html#d0e15100

I just get grizzly's request failed page if I try to use @Inject on a parameter. 如果尝试在参数上使用@Inject,我只会得到grizzly的请求失败页面。

Can someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

Main.java 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 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 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) {

    }
}

What you are doing with your binding is in the correct form. 您对绑定所做的操作的格式正确。

However, you are binding to the @Contract annotated interface "TodoDao", which means that it will look for the interface "TodoDao" for injection. 但是,您将绑定到带@Contract注释的接口“ TodoDao”,这意味着它将查找接口“ TodoDao”进行注入。 In your class TodoResource, you have "Dao<String>", which won't match. 在您的类TodoResource中,您有“ Dao <String>”,这将不匹配。 So, if you swap that out for TodoDao, it should find and replace it. 因此,如果将其换成TodoDao,它应该找到并替换它。

Now, if you want to use the Generic rather than the concrete class, you'll have to use a instantiated object with a wrapper. 现在,如果要使用泛型而不是具体的类,则必须使用带有包装器的实例化对象。 Something in the form of 形式的东西

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

Also, if you want some help binding automagically (kinda like Spring does), you can use the follow this article: http://www.justinleegrant.com/?p=516 另外,如果您需要自动绑定的帮助(类似于Spring,则可以),可以使用以下文章: http ://www.justinleegrant.com/?p=516

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM