Integrate in a CDI application that the container does not support it?

88 Views Asked by At

My application is deployed on JOnAS 5.2.2 that doesn't support CDI for EJB. I need to use CDI on that EJB. I know how to use CDI on WAR part of application but i don't know in EJB part.

Is there a way to add support for CDI for EJB application in a container that does not support it?

My boss won't upgrade the server for a version that supports it.

[EDIT] I use CDI-WELD: I've found a beginning of solution :

    //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

I've test with injection of another in DAOTest like this :

@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
}

}

It works, But the EntityManager is not resolve witjh @PersistenceContext. I think i must use the @Produce annotation but i don't understand how do it.

1

There are 1 best solutions below

0
On

You will need to use Deltaspike with BeanProvider functionality.

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