4 Replies Latest reply on Nov 15, 2012 12:01 PM by pilou13

    Seam-spring bean injection not working

    bitec

      Hi.


      I'm trying to use spring beans as cdi ones with help of seam-spring module. I managed to do this using appContext injection, but failed to inject the bean itself.:




      @ApplicationScoped
      public class SpringCdiProducer {
          @Produces
          @SpringContext
          @Web
          ApplicationContext context;
      
          @Produces
          @SpringBean
          RoleFactory roleFactory;
      }



      in my bean:


         


      @Inject
          @SpringContext
          @Web
          transient private ApplicationContext appContext; 
      
          @Inject
          transient private RoleFactory roleFactory; 
      ...
          public void someMethod() {
            this.role = roleFactory.getRole(roleName); // doesn't work
            this.role = appContext.getBean(RoleFactory.class).getRole(roleName); // works
          }





      I get the following exception:





      org.jboss.weld.exceptions.IllegalArgumentException: WELD-001324 Argument bean must not be null
           at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:714)
           at org.jboss.seam.spring.extension.SpringBeanProducer.produce(SpringBeanProducer.java:39)
           at org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:361)
           at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:67)
           at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:690)
           at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:772)
           at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:138)
           at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:872)
           at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:884)
           at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget$1$1.proceed(ManagedBean.java:182)
           at org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:134)



      Seems the default app context is not found, when I inject the spring bean. I thought that spring beans from spring web context should be discovered automatically with no manual actions.


      Thanks.

        • 1. Re: Seam-spring bean injection not working
          mbogoevici

          Hi Anton,


          At a first glance this looks valid. The fact that it does not work makes me want to look deeper into the issue, looks like a possible bug.


          Spring beans from the web context can be made available via @Produces @SpringBean, but I understand that this is exactly the part that does not work. I plan on providing automatic auto-importing, auto-vetoing for Spring beans coming from a web context (i.e. skipping the @Produces @SpringBean part) but that is still WIP, as a more elaborate solution is required due to Servlet and CDI container lifecycle issues.


          Cheers,
          Marius

          • 2. Re: Seam-spring bean injection not working
            bitec

            Marius,


            May be this will help.


            In web.xml I use standard configuration for spring initialization:




                    <context-param>
                      <param-name>contextConfigLocation</param-name>
                      <param-value>/WEB-INF/spring/applicationContext*.xml</param-value>
                 </context-param>
                 <listener>
                      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                 </listener>





            and WEB-INF/spring contains three app files:




            applicationContext.xml,
            applicationContext-roles.xml,
            applicationContext-security.xml



            and use SpringCdiProducer as mentioned above.


            Now I use injected ApplicationContext and it works ok. Thanks to this module now I can use Spring from CDI directly without touching FacesContext (FacesContextUtils.getWebApplicationContext(..)). This helps for unit testing business logics very much.

            • 3. Re: Seam-spring bean injection not working
              mbogoevici

              Anton,


              Thanks for the update and the interest in the Seam Spring module.


              I haven't been able to reproduce this issue so I created a basic example from our JBoss AS7 Spring archetype and added the Seam Spring module on top of it as a simple example of how this should work.


              You can find it here: https://github.com/mbogoevici/Seam-Spring-Basic-Example (use the 'glassfish' branch).


              You can see the CDI extension working when invoking the SimpleServlet (via $URL/servlet?user=jdoe), as classes in the org.jboss.seam.spring.example.cdi package are all CDI beans (reusing the Spring configured UserDao).


              I hope that this helps moving the discussion forward, please take a look and maybe you can spot the difference between your case and this.


              Please let me know how that worked.


              Cheers,
              Marius

              • 4. Re: Seam-spring bean injection not working
                pilou13

                Marius

                if you happen to have worked on this improvement, i would really be insterested :

                . I plan on providing automatic auto-importing, auto-vetoing for Spring beans coming from a web context (i.e. skipping the @Produces @SpringBean part) but that is still WIP, as a more elaborate solution is required due to Servlet and CDI container lifecycle issues.

                BTW what does WIP means ?

                 

                rgds

                Gauthier