Spring Book – Chapter 5 – Application Configuration – Simplified

Accessing Constants

For accessing constants in XML configuration you can use <util:constant/>. The usage of this tag is shown in Listing 5-25:

Listing 5-25. Using util-namespace to access constants in XML configuration

[java]public class LoyaltyContants {

public static final int JOINING_POINTS = 1000;

}[/java]
[xml]<bean id="LoyaltyService’ class="…">

<property name="joiningPoints">

<util:constant static-field="com.mybook.loyalty.constants.LoyaltyConstants.JOINING_POINTS"/>

</property>

</bean>[/xml]

In Listing 5-25 static-field is used to set property of a bean. This can also be used for constructor injection. <util:constant/> can also be used with enums in the same way as constants. Listing 5-26 shows using util-namespace to define a property using enums.

Listing 5-26. Using util-namespace to define enums in XML configuration

[java]public enum AccountType {

SILVER,

GOLD,

PLATINUM

}[/java]
[xml]<bean id="LoyaltyService’ class="…">

<property name="accountType">

<util:constant static-field="AccountLevel.GOLD" />

</property>

</bean>[/xml]

Accessing other bean properties

The <util:property-path/> tag lets you use properties of other beans already declared in your XML configuration. The <util:property-path/> requires that the first part of the path is actually a true bean property (getter and setter) on the target bean.

Listing 5-27. Usage of <util-property-path/> in XML configuration

[xml]<bean id="customerHome" class="com.mybook.loyalty.CustomerContact">

<property name="email" value="[email protected]" />

</bean>

<bean id="customerWork" class=" com.mybook.loyalty.CustomerContact">

<property name="email">

<util:property-path path="customerHome.email" />

</property>

</bean>[/xml]

Loading Properties

Using <util:properties/>, you can easily load a properties file into java.util.Properties instance and use it anywhere in your XML configuration.

Listing 5-28. Usage of <util:properties/> to load a properties file in classpath

[xml]<!– The following line creates a java.util.Properties instance with values loaded from the supplied location –>

<util:properties id="appConfiguration" location="classpath:com/mybook/loyalty/application.properties"/>[/xml]

The location attribute can use any pattern recognized by the Spring container. In Listing 5-28 I have used the pattern classpath, which means the Spring container will search the classpath to find the application.properties file.

Listing 5-29. Using <util:properties/> to load a properties file in classpath and fill a bean list property

[xml]<bean id="myBean" class="com.mybook.sample.MyClass">
<property name="myList">
<util:properties                                                                                                                                        location="classpath:com/mybook/loyalty/application.properties"/>
</property>
</bean>[/xml]

Page Visitors: 2900

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 *