通过多个级别的CDI注入? [英] CDI Injection through multiple levels?

查看:85
本文介绍了通过多个级别的CDI注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CDI初始化数据源(请参阅我以前的问题: DataSource自由配置文件服务器内的初始化错误)

 公共抽象类DAOBase {@Resource(name ="jdbc/Oracle",查找="jdbc/Oracle")私有数据源ds; 

当直接从REST类初始化DAO类时,这很好用:

  @Path("/audit")公共类AuditREST扩展RESTBase实现了AuditRESTInterface {@注入私有AuditDAO auditDAO =新的AuditDAO(); 

当REST类调用一个调用DAO的中间类时,我可以通过以下方式做到这一点:

 公共类JobConfigurationREST扩展了RESTBase实现的JobConfigurationRESTInterface {@注入私人JobRunner jr = new JobRunner();公共类JobRunner实现了Runnable {@注入私人JobConfigurationDAO jcDAO = new JobConfigurationDAO(); 

但是,JobRunner还包含一个可动态创建类(SampleModel)实例的方法.然后,SampleModel包含一个我需要注入的小岛.

 公共类SampleModel扩展了Step实现Model {@注入私有ModelDAO modelDAO = new ModelDAO(); 

我看不到在JobRunner和SampleModel之间添加链接的方法.我该怎么做?

或者,这是否有必要?我觉得必须指定REST类和DAO类之间的所有链接都太复杂了,我应该能够在某个地方声明SampleModel(可能是beans.xml?),并且容器应该为我处理一切./p>

我在这里简化问题.SampleModel是通过Job类创建的,而Job类是通过JobBuilder类中的方法创建的(我似乎也无法注入).

  JobBuilder jb = new JobBuilder();作业j = jb.buildJob(jc,jobContext); 

SampleModel也是通过buildJob方法内的深层反射来创建的.

 公共作业buildJob(JobConfiguration inJobConfiguration,JobContext inJobContext){stepList.add(ModelFactory.getInstance().getModel(sc.getImplementationName()));} 

在工厂内:

ret =(Model)Class.forName(tmpImplDetail.getClassName()).newInstance();

解决方案

您的问题似乎是您要将@Inject移到不是容器执行注入操作的组件的类中.

为您将新对象添加到注射点的方式掩盖了这一点.

  • 在确实支持注入的组件中,注入过程将在赋值后 进行并使其无害.
  • 在容器不为您注入的对象中,您将获得由new分配的对象,该对象将无法满足其自身的注入点.

如果您没有做奇怪的分配事情,那么您会问更多关于为什么我的@Inject字段为NULL?"的FAQ.

I am using CDI to initialize a DataSource (See my previous question: DataSource initialization error within liberty profile server)

public abstract class DAOBase {
    @Resource(name="jdbc/Oracle", lookup = "jdbc/Oracle")
    private DataSource ds;

This works well when the DAO class is initialized directly from the REST class:

@Path("/audit")
public class AuditREST extends RESTBase implements AuditRESTInterface {
    @Inject
    private AuditDAO auditDAO = new AuditDAO();

And when the REST class calls an intermediate class which calls the DAO I can do it by:

public class JobConfigurationREST extends RESTBase implements JobConfigurationRESTInterface {
    @Inject
    private JobRunner jr = new JobRunner();

public class JobRunner implements Runnable{
    @Inject
    private JobConfigurationDAO jcDAO = new JobConfigurationDAO();

However JobRunner also contains a method which creates some instances of a class (SampleModel) on the fly. SampleModel then contains a dao which I need to inject.

public class SampleModel extends Step implements Model {
    @Inject
    private ModelDAO modelDAO = new ModelDAO();

I can see no way of adding the link between JobRunner and SampleModel. How do I do this?

Alternatively, is this even necessary? I feel that having to specify all the links between the REST class and the DAO class is overly complicated, and I should be able to declare SampleModel somewhere (possibly beans.xml?), and the container should take care of everything for me.

EDIT:

I was simplifying the problem here. SampleModel is created via a Job class which is created via a method in a JobBuilder class (which I also can't seem to inject).

JobBuilder jb = new JobBuilder();
Job j = jb.buildJob(jc, jobContext);

SampleModel is also created via reflection deep within the buildJob method.

    public Job buildJob(JobConfiguration inJobConfiguration, JobContext inJobContext) {
stepList.add(ModelFactory.getInstance().getModel(sc.getImplementationName()));
    }

And within the Factory:

ret = (Model) Class.forName(tmpImplDetail.getClassName()).newInstance();

解决方案

Your problem appears to be that you want to move your @Inject into a class that is not a component the container performs injection on.

This is masked by the way you're assigning a new'ed up object to your injection point.

  • In components that actually do support injection, the injection process will happen after the assignment and make it harmless.
  • In objects that the container does not inject for you, you get an object allocated by new that will not have any of its own injection points satisfied.

If you were not doing the weird assignment thing, you'd be asking more of an FAQ of "why is my @Inject field NULL?"

这篇关于通过多个级别的CDI注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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