JBoss.org Community Documentation

9.4. Using Spring XML

Spring like JBoss Microcontainer allows POJOs to be created and configured using an XML schema. As the microcontainer largely supports the same set of features as Spring it is possible to use the spring-beans schema to define beans instead of the bean-deployer schema. This makes migrating services built and tested using Spring to JBoss Microcontainer very straightforward.

To define beans using the spring-beans schema you need to include spring-int.jar on the microcontainer classpath. You can then create a deployment descriptor containing a <beans> element at the root instead of the usual <deployment> element:


<beans xmlns="urn:jboss:spring-beans:2.0">
    <bean id="testBean" class="org.jboss.test.spring.support.SimpleBean">
        <constructor-arg index="2">
            <value>SpringBean</value>
        </constructor-arg>
        <constructor-arg index="0">
            <value>1</value>
        </constructor-arg>
        <constructor-arg index="1">
            <value>3.14159</value>
        </constructor-arg>

        <property name="mylist">
            <list value-type="java.lang.String">
                <value>onel</value>
                <value>twol</value>
                <value>threel</value>
            </list>
        </property>

        <property name="myset">
            <set value-type="java.lang.String">
                <value>ones</value>
                <value>twos</value>
                <value>ones</value>
            </set>
        </property>

        <property name="mymap">
            <map key-type="java.lang.String">
                <entry>
                    <key>
                        <value>test_key</value>
                    </key>
                    <value type="java.lang.String">myvalue</value>
                </entry>
            </map>
        </property>
    </bean>
</beans>

The namespace declaration xmlns="urn:jboss:spring-beans:2.0" tells the microcontainer that it should interpret the XML according to the spring-bean schema.

You can even mix beans declared using the bean-deployer schema with those declared using the spring-beans schema just like we did with the javabean schema. All that you need to do is include the correct namespace declarations in the relevant elements:

  • JBoss Microcontainer deployment containing Spring defined beans

    
    <deployment xmlns="urn:jboss:bean-deployer:2.0">

        <bean name="oldBean" class="org.jboss.test.spring.support.OldBean">
            <property name="testBean">
                <inject/>
            </property>
        </bean>

        <bean xmlns="urn:jboss:spring-beans:2.0" id="testBean" class="org.jboss.test.TestBean">
            <property name="mylist">
                <list value-type="java.lang.String">
                    <value>onel</value>
                    <value>twol</value>
                    <value>threel</value>
                </list>
            </property>
        </bean>

    </deployment> 
  • Spring defined deployment containing JBoss Microcontainer beans

    
    <beans xmlns="urn:jboss:spring-beans:2.0">

        <bean id="testBean" class="org.jboss.test.spring.support.SimpleBean">
            <property name="refBean">
                <ref bean="oldBean"/>
            </property>
        </bean>

        <bean xmlns="urn:jboss:bean-deployer:2.0" name="oldBean" class="org.jboss.test.spring.support.OldBean">
            <property name="myString">I'm an old bean</property>
        </bean>

    </beans>

Note

Adding spring-int.jar to the microcontainer classpath simply adds the ability to define beans using the spring-beans XML schema. It does not integrate JBoss Microcontainer with Spring libraries.