Spring Book – Chapter 5 – Application Configuration – Simplified

Loading List

Using <util:list/> you can easily create lists in your XML configuration without cluttering it.

Listing 5-30. Usage of <util:list/> to create a List instance with the supplied values

[xml]<!– creates a java.util.List instance with the supplied values –>
<util:list id="customerTypeMessageList">
<value>Hello, Silver Customer</value>
<value> Hello, Gold Customer</value>

<value> Hello, Platinum Customer</value>
</util:list>[/xml]

You can also explicitly control the exact type of List that will be instantiated and populated by using the ‘list-class’ attribute on the<util:list/> element. If no ‘list-class’ attribute is supplied, a List implementation will be chosen by the container, which is shown in Listing 5-31.

Listing 5-31. Usage of <util:list/> to create a ArrayList instance using the attribute list-class

[xml]<util:constant id="JoiningPoints" static-field="com.mybook.loyalty.constants.LoyaltyConstants.JOINING_POINTS"/>

<util:list id="A" list-class="java.util.ArrayList">
<value>myValue</value>
<ref bean="JoiningPoints"/>
</util:list>[/xml]

Loading Map

Using <util:map/> you can easily create map in your XML configuration. Even though there are traditional ways of creating maps in your configuration, this is a much cleaner and less clumsy way to configure the Spring container.

Listing 5-32. Usage of <util:map/> to create a Map instance with the supplied values

[xml]<util:map id="B">
<entry key="key1" value="value1"/>
<entry key="key2"><ref bean="JoiningPoints"/></entry>

</util:map>[/xml]

If you would like to control the map class creation, you have the provision of doing that using the attribute map-class.

Listing 5-33. Usage of <util:map/> to create a HashMap instance sing the attribute map-class

[xml]<util:map id="B" map-class="java.util.HashMap">
<entry key="key1" value="value1"/>
<entry key="key2"><ref bean="JoiningPoints"/></entry>

</util:map>[/xml]

Loading Set

You definitely won’t want to overlook the <util:set/> option if your application requires it. Similar to list and map, set also provides an attribute namely set-class by which you can control the instance that <util:set/> generates inside the Spring container.

Listing 5-34. Usage of <util:set/> to create a Set instance with the supplied values

[xml]<util:set id="S">
<value>foo</value>
<ref bean="X"/>

</util:set>[/xml]

Listing 5-35. Usage of <util:set/> to create a HashSet instance using the attribute set-class

[xml]<util:set id="S" set-class="java.util.HashSet">
<value>foo</value>
<ref bean="X"/>

</util:set>[/xml]

Compound property names

Compound or nested property names are perfect for simplifying the XML configuration in a traditional way, for this approach to work properly, none of the path components, except the final property names, can be null. Otherwise a NullPointerException will result.

Properties can get and set values of beans inside other beans. There is no limit to the number of levels you can keep calling nested properties. Nested properties can also be used to access values in a List or Map.

Listing 5-36. Usage of compound property names to reduce the amount of configuration usually required in a traditional way of doing things

[java]public class ClassA {

public ClassB classB;

//… getters and setters

}

Public class ClassB {

Public ClassC classC;

//..getters and setters

}

Public class ClassC {

Int myInt;

//..getters and setters

}[/java]
[xml]<bean id="classA">

<property name="classB.classC.myInt" value="100" />

</bean>[/xml]

In Listing 5-36 myInt will be set to 100 but classB and classC should not be null values.

Page Visitors: 3748

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 5 – Application Configuration – Simplified

  1. Hi Tomcy,

    All looks great and thanks for your time and efforts.

    One small suggestion if you could provide single page view for all the Spring tutorials would be great. May print option as well.

    Thanks & regards,
    AA

Leave a Reply

Your email address will not be published. Required fields are marked *