集成在容器不支持的CDI应用程序中?

时间:2014-03-10 07:09:41

标签: java-ee ejb cdi jonas

我的应用程序部署在JOnAS 5.2.2上,不支持CDI EJB。我需要在CDI上使用EJB。我知道如何在应用程序的WAR部分使用CDI,但我不知道EJB部分。

有没有办法在CDI应用程序的EJB应用程序中添加对不支持它的容器的支持?

我的老板不会为支持它的版本升级服务器。

[编辑] 我使用CDI-WELD:我找到了解决方案的开始:

    //CDI uses an AnnotatedType object to read the annotations of a class
    AnnotatedType<DAOTest> type = beanManager.createAnnotatedType(DAOTest.class);
    //The extension uses an InjectionTarget to delegate instantiation, dependency injection 
    //and lifecycle callbacks to the CDI container
    InjectionTarget<DAOTest> it = beanManager.createInjectionTarget(type);
    //each instance needs its own CDI CreationalContext
    CreationalContext ctx = beanManager.createCreationalContext(null);
    //instantiate the framework component and inject its dependencies
    test = it.produce(ctx);  //call the constructor
    System.out.println("instance" + test);
    it.inject(test, ctx);  //call initializer methods and perform field injection
    it.postConstruct(test);  //call the @PostConstruct method

    test.test();

    it.preDestroy(test);  //call the @PreDestroy method
    it.dispose(test);  //it is now safe to discard the instance
    ctx.release();  //clean up dependent objects

我在DAOTest中注射了另一个像这样的测试:

@Named
@Dependent
public class DAOTest implements Serializable {
private static final long serialVersionUID = 1L;


@Persitence(value = "CDI-ejb")
private EntityManager em;

@Inject 
private User user; 

public void test(){
    System.out.println(user.getName());

    em.getClass();
}

public EntityManager getEm() {

    return em;
}

public void setEm(EntityManager em) {
    this.em = em;
}

public DAOTest() {
    // TODO Auto-generated constructor stub
}

}

它可以工作,但是EntityManager无法解析@PersistenceContext。我想我必须使用@Produce注释,但我不明白它是怎么做的。

1 个答案:

答案 0 :(得分:0)

您需要使用Deltaspike和BeanProvider功能。

http://deltaspike.apache.org/documentation.html