7 Replies Latest reply on Dec 21, 2012 8:03 AM by jharting

    CDI Extension, @Inject and Interceptors

    veitg

      Hi.

       

      I'm using JBoss 7.1.1 Final and currently implementing a CDI Extension. During development I encountered some caveats that I couldn't find in any documentation and cost me some time to figure out.

       

      1) @Inject for Event<T> and BeanManager (or in general?) doesn't seem to work in Extensions - they're always null. I found out somewhere that you can let inject them as a 2nd parameter in a Observer method.

      2) InterceptorBinding annotations don't seem to work in Extensions.

       

      Is there any documentation that explicitly points out that some standard functions that work in normal CDI beans don't work in Extensions? Google didn't help me much .

       

      Thanks

        • 1. Re: CDI Extension, @Inject and Interceptors
          vladcrc

          Hi,

           

          I am also having the problem that Interceptors don't work in an extension.

           

          https://community.jboss.org/message/765016

           

          And coulnd't find an answer...

          • 2. Re: CDI Extension, @Inject and Interceptors
            codeprince

            Hi Veit,

             

             

            Veit Guna による書き込み:

             

            Hi.

             

            I'm using JBoss 7.1.1 Final and currently implementing a CDI Extension. During development I encountered some caveats that I couldn't find in any documentation and cost me some time to figure out.

             

            1) @Inject for Event<T> and BeanManager (or in general?) doesn't seem to work in Extensions - they're always null. I found out somewhere that you can let inject them as a 2nd parameter in a Observer method.

            BeanManager can be obtained as 2nd parameter as following:

             

            void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bdd, BeanManager beanManager){

                    ...

                    this.beanManager = beanManager;

                }

            • 3. Re: CDI Extension, @Inject and Interceptors
              jharting

              CDI extensions are not managed beans and thus are not provided with CDI services. The only thing you can inject is the BeanManager as shown above. However, you should not use it to lookup beans, decorators, etc. until the container is initialized.

              • 4. Re: CDI Extension, @Inject and Interceptors
                alberto_souza

                I am not so sure about that. The AfterDeploymentValidation observer can receive managed objects, at least in my tests. I don't know if this is a weld feature or a spec feature.

                • 5. Re: CDI Extension, @Inject and Interceptors
                  jharting

                  That is an unspecified feature which applications should not rely on. It is going to be removed.

                  • 6. Re: CDI Extension, @Inject and Interceptors
                    codeprince

                    Jozef Hartinger による書き込み:

                     

                    That is an unspecified feature which applications should not rely on. It is going to be removed.

                    Hi Jozef,

                     

                    What you said "It is going to be removed" is that in the future, beanManager can not be obtained from beforeBeanDiscovery method?

                     

                    In addition, just as you said, beanManager from beforeBeanDiscovery method is a extension beanManager, and there is a possibility that a user can not

                    use the beanManager to lookup managed beans(or some beans related user-defined classes) , of course , this also depends on how deployment.loadBeanDeploymentArchive(Class<?> beanClass)

                    is implemented?

                     

                    I have found a scene on glassfish weld integration: if you obtain a beanManager from beforeBeanDiscovery method which is in glassfish cdi extension, you can not look up managed beans because the

                    extension deployment's beanDeploymentArchive has not visibility to managed beans.

                     

                    Thanks

                    --Tang

                    • 7. Re: CDI Extension, @Inject and Interceptors
                      jharting

                      No, the possibility of injecting other instances to container lifecycle event observers is non-portable going to be removed. BeanManager will still be injectable though certain methods will not be allowed to be called during container initialization. See https://issues.jboss.org/browse/CDI-274 for details.