Dropwizard HK2注射液 [英] Dropwizard HK2 injection

查看:172
本文介绍了Dropwizard HK2注射液的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用dropwizard时非常新。目前我正在尝试实现HK2依赖注入。这在资源中工作得很好,但它不能在资源之外工作。以下是我正在做的事情:

 客户端客户端=新的JerseyClientBuilder(环境).using(configuration.getJerseyClientConfiguration())。建立( contentmoduleservice); 

// DAOs
ContentModuleDAO contentModuleDAO = new ContentModuleDAO(hibernate.getSessionFactory());
ModuleServedDAO moduleServedDAO = new ModuleServedDAO(hibernate.getSessionFactory());

//经理
ContentModuleManager moduleManager = new ContentModuleManager();
EntityTagManager eTagManager = new EntityTagManager();
ProposalManager proposalManager = new ProposalManager(客户端,配置);

environment.jersey()。register(new AbstractBinder(){
@Override
protected void configure(){
bind(eTagManager).to(EntityTagManager。 class);
bind(contentModuleDAO).to(ContentModuleDAO.class);
bind(moduleServedDAO).to(ModuleServedDAO.class);
bind(proposalManager).to(ProposalManager.class) ;
bind(moduleManager).to(ContentModuleManager.class);
}
});

我创建了我想要注入的类的实例并绑定它们。



在我的资源中注入有效:

  @Api 
@Path( / api / contentmodule)
public class ContentModuleResource {

static final Logger LOG = LoggerFactory.getLogger(ContentModuleResource.class);
static final int MAX_PROPOSALS_PER_MODULE = 10;

@Inject
private ContentModuleDAO contentModuleDAO;

@Inject
private EntityTagManager eTagManager;

@Inject
private ProposalManager proposalManager;

@Inject
private ContentModuleManager contentModuleManager;

所有这些变量都填充了正确类的实例。



问题是:ContentModuleManager还应该通过注入获得这些类中的一些:

  public class ContentModuleManager {

@Inject
private ContentModuleDAO contentModuleDAO;

@Inject
private ProposalManager proposalManager;

@Inject
private ModuleServedDAO moduleServedDAO;

但这些都是空的。有人可以解释一个dropwizard noob为什么会发生这种情况以及如何解决这个问题? :D



谢谢!

解决方案

如果要实例化服务自己然后它不会经历DI生命周期,永远不会被注入。如果您只是将服务注册为类,则可以让容器创建服务

  bind(ContentModuleManager.class)
.to(ContentModuleManager.class)
.in(Singleton.class);

另一方面,如果您自己创建所有服务并且所有服务都可用,为什么你根本不使用DI容器?只需通过构造函数传递所有服务。无论您是使用构造函数注入 1 还是手动传递构造函数,通过构造函数获取服务都是很好的做法,因为它可以更轻松地测试服务。 / p>




1 - 构造函数注入

 私人服务; 

@Inject
public OtherService(服务服务){
this.service = service;
}


im pretty new in working with dropwizard. Currently I'm trying to implement the HK2 dependency injection. That works pretty fine inside a resource but it doesn't work outside of a resource. Here is what I'm doing:

Client client = new JerseyClientBuilder(environment).using(configuration.getJerseyClientConfiguration()).build("contentmoduleservice");

    //DAOs
    ContentModuleDAO contentModuleDAO = new ContentModuleDAO(hibernate.getSessionFactory());
    ModuleServedDAO moduleServedDAO = new ModuleServedDAO(hibernate.getSessionFactory());

    //Manager
    ContentModuleManager moduleManager = new ContentModuleManager();
    EntityTagManager eTagManager = new EntityTagManager();
    ProposalManager proposalManager = new ProposalManager(client, configuration);

    environment.jersey().register(new AbstractBinder() {
        @Override
        protected void configure() {
            bind(eTagManager).to(EntityTagManager.class);
            bind(contentModuleDAO).to(ContentModuleDAO.class);
            bind(moduleServedDAO).to(ModuleServedDAO.class);
            bind(proposalManager).to(ProposalManager.class);
            bind(moduleManager).to(ContentModuleManager.class);
        }
    });

I create Instance of the classes I want to be injectible and bind them.

Inside my resource the injection works:

@Api
@Path("/api/contentmodule")
public class ContentModuleResource {

    static final Logger LOG = LoggerFactory.getLogger(ContentModuleResource.class);
    static final int MAX_PROPOSALS_PER_MODULE = 10;

    @Inject
    private ContentModuleDAO contentModuleDAO;

    @Inject
    private EntityTagManager eTagManager;

    @Inject
    private ProposalManager proposalManager;

    @Inject
    private ContentModuleManager contentModuleManager;

All these variables are filled with an Instance of the right class.

The problem is: The ContentModuleManager should also get some of these classes via injection:

public class ContentModuleManager {

@Inject
private ContentModuleDAO contentModuleDAO;

@Inject
private ProposalManager proposalManager;

@Inject
private ModuleServedDAO moduleServedDAO;

But those are null. Can somebody explain a dropwizard noob why this happens and how I can fix this? :D

Thanks!

解决方案

If you are going to instantiate the service yourself then it is not going to go through the DI lifecycle and will never be injected. You can let the container create the service if you just register the services as a class

bind(ContentModuleManager.class)
    .to(ContentModuleManager.class)
    .in(Singleton.class);

On the other hand, if you are creating all the services yourself and you have all of them available, why don't you just not use the DI container at all? Just pass all the services through the constructor. Whether you are using constructor injection1 or manually passing through the constructor, getting the service through the constructor is good practice anyway, as it allows for easier testing of the service.


1 - Constructor injection

private Service service;

@Inject
public OtherService(Service service) {
   this.service = service;
}

这篇关于Dropwizard HK2注射液的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆