February 17, 2010

Configuring Beans in the Spring IoC Container

« Spring Roo Tutorial | Main | Spring Hibernate - LazyInitializationException »
Spring is a powerful IoC container to manage components. To utilize the container components, you have to configure your beans. In this post I will show you how to configure you beans using XML. You probably already know how to configure beans using setter injection:
package com.mycompany.myproject;
public class MyService {
	private String myValue;
	public void setMyValue(String myValue) {
		this.myValue = myValue;
	}
}
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean name="myService" class="com.mycompany.myproject.MyService">
        <property name="myValue">
            <value>someValue</value>
        </property>
    </bean>
</beans>
Or using shortcuts:
<bean name="myService" class="com.mycompany.myproject.MyService" >
    <property name="myValue" value="someValue"/>
</bean>
But here are some configuration options you probably haven't used yet:
Spring allows you to use constructor injection:
package com.mycompany.myproject;
public class MyService {
	private String myValue;
	public MyService(String myValue) {
		super();
		this.myValue = myValue;
	}
}
<bean name="myService" class="com.mycompany.myproject.MyService" >
    <constructor-arg>
        <value>someValue</value>
    </constructor-arg>
</bean>
Or using shortcuts:
<bean name="myService" class="com.mycompany.myproject.MyService" >
    <constructor-arg value="someValue" />
</bean>
An other convenient shortcut to define properties is by using the p schema to define bean properties:
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean name="myService" class="com.mycompany.myproject.MyService" p:myValue="someValue" />
</beans>
You can use lists as properties:
package com.mycompany.myproject;
import java.util.List;
public class MyService {
	private List myObjects;
	public void setMyObjects(List myObjects) {
		this.myObjects = myObjects;
	}
}

<bean name="myService" class="com.mycompany.myproject.MyService">
    <property name="myObjects">
        <list>
            <value>someValue</value>
            <bean class="java.net.URL">
                <constructor-arg value="http" />
                <constructor-arg value="developers-blog.org" />
                <constructor-arg value="/" />
            </bean>
            <null />
        <list>
    </property>
</bean>
The definition of an array in the bean configuration is identical to a list.
Another common collection type is a set:
package com.mycompany.myproject;
import java.util.Set;
public class MyService {
	private Set myObjects;
	public void setMyObjects(Set myObjects) {
		this.myObjects = myObjects;
	}
}

<bean name="myService" class="com.mycompany.myproject.MyService">
    <property name="myObjects">
        <set>
            <value>someValue</value>
            <bean class="java.net.URL">
                <constructor-arg value="http" />
                <constructor-arg value="developers-blog.org" />
                <constructor-arg value="/" />
            </bean>
            <null />
        <set>
    </property>
</bean>
Maps are defined by the <map> with multiple <entry> tags as children. Each entry contains a key and a value:
package com.mycompany.myproject;
import java.util.Map;
public class MyService {
	private Map myObjects;
	public void setMyObjects(Map myObjects) {
		this.myObjects = myObjects;
	}
}
<bean name="myService" class="com.mycompany.myproject.MyService">
    <property name="myObjects">
        <map>
            <entry>
                <key>A</key>
                <value>someValue</value>
            </entry>
            <entry>
                <key>B</key>
                <bean class="java.net.URL">
                    <constructor-arg value="http" />
                    <constructor-arg value="developers-blog.org" />
                    <constructor-arg value="/" />
                </bean>
            </entry>
        <map>
    </property>
</bean>
Last but not least you can use java.util.Properties collection very similar to a map:
package com.mycompany.myproject;
import java.util.Properties;
public class MyService {
	private Properties myProperites;
	public void setMyProperites(Properties myProperites) {
		this.myProperites = myProperites;
	}
}
<bean name="myService" class="com.mycompany.myproject.MyService">
    <property name="myProperties">
        <props>
            <prop key="A" value="someValue"/>
            <prop key="B" value="http://developers-blog.org/" />
        </props>
    </property>
</bean>
If you define your beans with inheritance, a child bean’s collection can be merged with that of its parent by setting the merge attribute to true. For a <list> collection, the child elements will be added.

Technorati Tags:

Posted by eduard.hildebrandt at 7:05 PM in Spring

 

[Trackback URL for this entry]

Your comment:

(not displayed)
 
 
 

Live Comment Preview: