2 Replies Latest reply on Jul 20, 2011 10:49 AM by Andrew Murphy

    CDI bean injection problem

    Andrew Murphy Newbie

      Apologies if this query should be put to the user forum, but I'm not sure if what I face is a bug, or me just being dumb!


      I have a bean, DownloadJob, which implements the quartz (timer) Job interface method execute(...). The bean has the @ManagedBean annotation. The problem I face is I have a number of CDI dependant beans declared within the DownloadJob bean which are not being injected i.e. all references to CDI beans remain 'null'. I suspect the underlying cause is the container is unaware of the DownloadJob bean (despite being annotated with @ManagedBean) as the quartz library instantiates it when a timer is triggered. Nonetheless my expectation is the container should still resolve calls/references to the other CDI managed beans under its control. Am I mistaken or naive?


      Rather than paste a zillion of lines of code I have created the simple test case (zip attached) which replicates this issue by instantiating a bean with CDI dependencies using the new keyword. An excerpt of the test case is below.


      I have the latest JBoss AS7.1 snapshot which I understand addresses a recent CDI injection bug, so any insight/guidance will be appreciated.


      Thank you, Andrew






      public class SimpleCDITest {





          private InjectableEJB1 injectableEJB1;



          public void injectedManagedBeanDependencyTest() {






          public void instantiatedBeanDependencyTest() {

               final InjectableEJB2 injectableEJB2 = new InjectableEJB2();

              // Injected dependencies into InjectableEJB2 are null...

              // ... assert fails





        • 1. Re: CDI bean injection problem
          Stuart Douglas Master

          You can't create an EJB with the new operator, you need to inject the bean or get it from JNDI for injection to take place.

          • 2. Re: CDI bean injection problem
            Andrew Murphy Newbie

            Thank you Stuart. Understood.


            Actually, I've just realised my test case is incorrectly specified, not that it makes a difference. It should inject and instantiate a POJO annotated with @ManagedBean to mimick my DownloadJob bean. It is the POJO bean that has dependencies on other other injected EJBs which are not resolved when the new operator is used to instantiate the POJO (as is the case when quartz instantiates DownloadJob in response to a timer event).


            I guess in this case I will need to use a producer method to inject the dependencies as I have no control over the creation of the DownloadJob POJO? I will have to try that out.


            The move to AS7 and CDI has been such a steep learning curve so thanks for taking the time to answer what must seem a trivial question!


            Message was edited by: Andrew Murphy ...my final word on this topic. I held the mistaken belief it was possible to simply inject managed EJBs etc into a simple POJO/non-managed bean! Oh boy, I'm feeling rather sheepish now having put the question to the forum. I've learned my lesson and include a modified test case which uses JNDI to obtain a reference to a managed bean so others don't repeat my mistake. This approach obviously solves my original problem where quartz controls the instantiation of timer job instances which reference managed EJBs.