Spring Core

How to use Collection element <props/> in Spring ?

Create a class

package com.roytuts.spring.collection;
import java.util.Properties;
public class SpringCollection {
    private Properties properties;
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

Configure metadata

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="springCollection" class="com.roytuts.spring.collection.SpringCollection">
        <property name="properties">
            <props>
                <prop key="administrator">administrator@example.org</prop>
                <prop key="support">support@example.org</prop>
                <prop key="development">development@example.org</prop>
                <prop key="sitenv">sitenv@example.org</prop>
                <prop key="uatenv">uatenv@example.org</prop>
            </props>
        </property>
    </bean>
</beans>

For more information please read https://roytuts.com/collection-element-props-in-spring/

How to use Collection element <list/> in Spring ?
Create a class

package com.roytuts.spring.collection;
import java.util.List;
public class SpringCollection {
    private List<String> list;
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
}

Configure metadata

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="springCollection" class="com.roytuts.spring.collection.SpringCollection">
        <!-- results in a list(java.util.List) call -->
        <property name="list">
            <list>
                <value>A</value>
                <value>B</value>
                <value>C</value>
                <value>D</value>
            </list>
        </property>
    </bean>
</beans>

For more information please read https://roytuts.com/collection-element-list-in-spring/

How to use Collection element <map/> in Spring ?
Create a class

package com.roytuts.spring.collection;
import java.util.Map;
public class SpringCollection {
    private Map<String, String> map;
    public Map<String, String> getMap() {
        return map;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
}

Configure metadata

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="springCollection" class="com.roytuts.spring.collection.SpringCollection">
        <property name="map">
            <map>
                <entry key="key1" value="value1" />
                <entry key="key2" value="value2" />
                <entry key="key3" value="value3" />
                <entry key="key4" value="value4" />
            </map>
        </property>
    </bean>
</beans>

For more information please read https://roytuts.com/collection-element-map-in-spring/

How to use Collection element <set/> in Spring ?
Create a class

package com.roytuts.spring.collection;
import java.util.Map;
public class SpringCollection {
    private Set<String> set;
    public Set<String> getSet() {
        return set;
    }
    public void setSet(Set<String> set) {
        this.set = set;
    }
}

Configure metadata

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="springCollection" class="com.roytuts.spring.collection.SpringCollection">
        <property name="set">
            <set>
                <value>just some string</value>
                <value>another some string</value>
                <value>more value string</value>
            </set>
        </property>
    </bean>
</beans>

For more information please read https://roytuts.com/collection-element-set-in-spring/

What is Collection merging in Spring ?

Since Spring 2.0, the container supports merging of collections. You can define <prop/>, <list/>, <map/> and <set/> of parent-style and then you can inherit the child-style element and override values of each type from parent collections. That is, the child collection’s values are the result of merging the elements of the parent and child collections, with the child’s collection elements overriding values specified in the parent collection.

Create a class

package com.roytuts.spring.collection;
import java.util.Properties;
public class SpringCollectionMerge {
    private Properties properties;
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

Configure metadata

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="springCollectionParent" class="com.roytuts.spring.collection.SpringCollectionMerge">
        <property name="properties">
            <props>
                <prop key="administrator">administrator@example.org</prop>
                <prop key="support">support@example.org</prop>
                <prop key="development">development@example.org</prop>
                <prop key="sitenv">sitenv@example.org</prop>
                <prop key="uatenv">uatenv@example.org</prop>
            </props>
        </property>
    </bean>
    <bean id="springCollectionChild" parent="springCollectionParent">
        <property name="properties">
            <!-- the merge is specified on the *child* collection definition -->
            <props merge="true">
                <prop key="administrator">administrator@example.org</prop>
                <prop key="support">support@example.co.uk</prop>
                <prop key="sitenv">sales@example.co.in</prop>
                <prop key="uatenv">uatenv@example.org.uk</prop>
            </props>
        </property>
    </bean>
</beans>

Output will be

uatenv => uatenv@example.org.uk
support => support@example.co.uk
administrator => administrator@example.org
development => development@example.org
sitenv => sales@example.co.in

Please use merge=”true” in the child bean in metadata configuration file to merge the parent collections with child collections.

The child Properties collection’s value set inherits all property elements from the parent <props/>,
and the child’s values for administrator, support, uatenv and sitenv override the values in the parent collection.

For more information please read https://roytuts.com/collection-merging-in-spring/

What are the limitations of collection merging in Spring ?

You cannot merge different types collections together such as you cannot merge Map with List and if you do so you will an appropriate Exception. The merge attribute must be specified on the lower, inherited,
child definition. If you specify the merge attribute on a parent collection definition then it is redundant and will not result in the desired merging.

How to set empty or null value to a property in Spring ?

You can set empty or null value to a property using Spring metadata configuration in XMl file.

The following snippet sets the email property to the empty String value (“”).

<bean class="com.roytuts.spring.SomeClass">
    <property name="email" value=""/>
</bean>

The above code is equivalent to Java code: SomeClass.setEmail(“”).

The <null/> element handles null values. The following snippet sets the email property to the null value.

<bean class="com.roytuts.spring.SomeClass">
    <property name="email"><null/></property>
</bean>

The above configuration is equivalent to the following Java code: SomeClass.setEmail(null).

What is p-namespace in Spring ?

The p-namespace is supported since Spring 2.0. The p-namespace enables you to use the bean element’s attributes, instead of nested <property/> elements, to describe your property values and/or collaborating beans.

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- standard XML config -->
    <bean name="standard" class="com.roytuts.ExampleClass">
        <property name="email" value="contact@roytuts.com"/>
    </bean>
    <!-- using p-namespace XML config -->
    <bean name="p-namespace" class="com.roytuts.ExampleClass"
        p:email="contact@roytuts.com"/>
    <bean name="animesh-standard" class="com.roytuts.Student">
        <property name="name" value="Animesh Baul"/>
        <property name="email" value="animesh@roytuts.com"/>
        <property name="age" ref="29"/>
    </bean>
    <bean name="animesh-namespace" class="com.roytuts.Student"
        p:name="Animesh Baul"
        p:email="animesh@roytuts.com"
        p:age-ref="ageBean"/>
    <bean name="ageBean" class="com.roytuts.Student">
        <property name="age" value="29"/>
    </bean>
</beans>