3 Replies Latest reply: May 19, 2011 2:22 PM by piobair RSS

JBoss 5, Spring 3 Autowiring beans

piobair Newbie

Well, I've looked at all the discussions, docs etc. that I can get my hands on and I'm still having problems getting the beans autowired. I can see the beans being instantiated at deployment time, but they're just not making it into my ServiceBean autowired.

 

I've tried using both the SpringLifecycleInterceptor.class/@Spring annotations and the SpringBeanAutowiringInterceptor.class/@Autowire methods. No dice.

 

Here's my configs and annotations.

 

jboss-spring.xml

 

<?xml version="1.0" encoding="UTF-8"?>

 

<beans xmlns="http://www.springframework.org/schema/beans"

           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

            xmlns:context="http://www.springframework.org/schema/context"

 

           xsi:schemaLocation="http://www.springframework.org/schema/beans

            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          

            http://www.springframework.org/schema/context

            http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 

    <description>BeanFactory=(reportsApp)</description>

 

 

    <context:annotation-config/>

 

    <bean id="connectionFactoryList" class="com.likewise.lucy.util.riak.RiakConnectionFactory">

         <property name="clients">

             <list>

                 <ref bean="riakClient1"/>

                 <ref bean="riakClient2"/>

             </list>

         </property>

     </bean>

 

     <bean id="riakClient1" class="com.basho.riak.client.RiakClient">

         <constructor-arg ref="riakConfig1"/>

     </bean>

 

     <bean id="riakClient2" class="com.basho.riak.client.RiakClient">

         <constructor-arg ref="riakConfig2"/>

     </bean>

 

     <bean id="riakConfig1" class="com.basho.riak.client.RiakConfig">

             <property name="url" value="http://10.100.1.119:8091/riak"/>

             <property name="timeout" value="10000"/>

             <property name="maxConnections" value="10"/>

     </bean>

 

     <bean id="riakConfig2" class="com.basho.riak.client.RiakConfig">

             <property name="url" value="http://10.100.1.117:8091/riak"/>

             <property name="timeout" value="10000"/>

             <property name="maxConnections" value="10"/>

     </bean>

    <!--  import resource="applicationContext.xml"/ -->

</beans>

 

 

and class snippet.

 

@Service

@Management(RiakEventsForUserMBean.class)

 

@Interceptors(SpringBeanAutowiringInterceptor.class)

public class RiakEventsForUser implements RiakEventsForUserMBean {

    private boolean started = false;

 

    @Autowired

    //@Spring(jndiName="reportsApp",bean="connectionFactoryList")

    private RiakConnectionFactory factory;

    // private ApplicationContext ctx;

 

 

I'm sure I'm just missing something simple, but can't seem to figure out what.

  • 1. JBoss 5, Spring 3 Autowiring beans
    Ales Justin Master
    but they're just not making it into my ServiceBean autowired.

    How do you see that ServiceBean is handled by Spring?

    Or can you debug its handling of @Autowired?

  • 2. JBoss 5, Spring 3 Autowiring beans
    Marius Bogoevici Expert

    Hm, I tried to reproduce the issue myself using the Spring deployer (on which the example seems to be initially based), but I see a different issue: the Spring lifecycle interceptor is applied correctly and tries to inject the bean, but because the managed bean is deployed before the Spring application context is bootstrapped, the deployment fails (the lifecycle interceptor cannot find the object in JNDI). One solution is to change the ordering of the deployments.

     

    I suppose that switching to @Autowired (as you did for the class) would help, but you would also need to set up a ClasspathSingletonBeanFactoryLocator - see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/ejb.html#ejb-implementation (the EJB2 section explains how to set it up, and the EJB3 section explains how to use it for EJB3).

  • 3. JBoss 5, Spring 3 Autowiring beans
    piobair Newbie

    Trying the @Autowired approach and having no luck with that either. It's a little unclear from the docs referenced exactly what ejb 3.0 requires. I have a beanRefContext.xml file defined as so:

     

    <beans>

        <bean id="beanFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">

            <constructor-arg value="applicationContext.xml" />

        </bean>

    </beans>

     

    It's being deployed in the META-INF directory (I tried it at root also - unclear where this should really go).

     

    I have

    @Interceptors(SpringBeanAutowiringInterceptor.class)

     

    defined in the ServiceBean implementation and @Autowired on the fields to autowire.

     

    It _appears_ from the docs that this is all that should be necessary, but it's still not working. Anyway, I think I'm doing what I'm supposed to, but still end up with NPEs when referencing objects that should be autowired.