Spring Book – Chapter 5 – Application Configuration – Simplified

Overriding Bean definition

Any given Spring context can only have one bean for any given id or name. In the case of the XML id attribute configuration, this is enforced by the schema validation. In the case of the name attribute, this is enforced by Spring container’s logic.

However, if a context is constructed from two different XML descriptor files, and an id is used by both files, then one will “override” the other. The exact behavior depends on the ordering of the files when they get loaded by the application context.

Although it is possible, it is not recommended. It is error-prone and fragile. Listing 5-11 shows how this can be achieved.

Listing 5-11. Overriding beans in different XML configuration

legacy-jar-config.xml

[xml]<bean id="inheritedBean"  >
<property name="parentProperty1" value="parentProperty1"/>
<property name="parentProperty2" value="parentProperty2"/>
</bean>[/xml]

legacy-jar-override-config.xml

[xml]<bean id="inheritedBean"  class="com.mybook.sample.InheritedBean">
<property name="parentProperty1" value="parentProperty1"/>
<property name="parentProperty2" value="parentProperty2"/>
</bean>[/xml]

After defining beans in different files with same id attribute as shown in Listing 5-11, you can load the XML configuration in whatever order you would like your bean to be initialized and Spring container will take the bean that is loaded later in the lifecycle and maintain it. You also have different way by which to override or rather inherit the bean definition, as shown in Listing 5-12. These ways of overriding/inheriting were discussed earlier in the section “Bean definition inheritance.” I refer to it again so that you are clear about the different ways that you can override bean instances in Spring.

Listing 5-12. Different way of overriding bean properties using the parent attribute by the sub bean

[xml]<bean id="inheritedBean" abstract="true">
<property name="parentProperty1" value="parentProperty1"/>
<property name="parentProperty2" value="parentProperty2"/>
</bean>

<bean id="inheritedBeanWithDifferentClass"
parent="inheritedBean" init-method="initialize">
<property name="parentProperty1" value="overrideParentProperty1"/>
<!– the parentProperty2 property value of parentProperty2 will be inherited from  parent –>
</bean>[/xml]

In Listing 5-12, bean inheritedBean will not be initialized as it is declared as abstract.

XML Simplification Using Namespace

XML configuration can be greatly simplified by using namespaces provided by Spring framework. Chapter 20 examines namespaces in detail, so in this section I will cover only the important namespaces that are relevant to the objectives of this chapter, simplifying XML configuration in Spring. Most of the projects in spring have their own namespaces, which can be used to simplify and ease the XML configuration in spring to a greater extent. But certain namespaces are used in turn by all other projects so as to simplify the configurations. In this section I will be giving you more details on three such namespaces, namely p-namespace, c-namespace, and util-namespace.

p-namespace

This namespace is used as a shortcut to specifying bean properties in XML configuration. Properties of a bean can be specified as values or reference. This namespace allows you to declare both values and reference properties for a bean. Listing 5-13 shows usage of p-namespace in specifying values and references while configuring a bean. Before you can use this namespace in your XML configuration, you need to explicitly put the namespace definition in your XML file. Unlike other spring namespaces this namespace is not specified in an XSD file, so other than the namespace declaration, an additional schemeLocation entry for this is not required.

Listing 5-13. Namespace declaration for using p-namespace in your XML configuration

[xml]<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 configurations –>

</beans>

Listing 5-14. Specifying a value as property to a bean without using p-namespace

<bean id="UserDetails" class="com.mybook.model.UserDetails">

<property name="firstName" value="Isaac" />

<property name="lastName" value="Newton" />

</bean>[/xml]

Listing 5-15. Specifying a value as property to a bean using p-namespace

[xml]<bean id="UserDetails" p:firstName="Isaac" p:lastName="Newton" />[/xml]

Specifying properties using p-namespace requires the following format

[xml]p:<property name>=<value required>[/xml]

Listing 5-16. Specifying a reference as property to a bean without using p-namespace

[xm]<bean id=”UserAccount” class=”com.mybook.model.UserAccount”>

<property name=”userDetails” ref=”UserDetails” />

<property name=”accountDetails” ref=”AccountDetails” />

</bean>[/xml]

Listing 5-17. Specifying a reference as property to a bean using p-namespace

[xml]<bean id="UserAccount" p:userDetails-ref="UserDetails" p:accountDetails-ref="AccountDetails" />[/xml]

Reference to other beans using p-namespace requires the following format

[xml]p:<property name>-ref=<reference name of bean>[/xml]

Note: Let’s assume you have a property named “userDetails” in your class, which uses camel case notation. In this case you can use p-namespace in the following two ways:

[xml]p:userDetails-ref=<reference>[/xml]

or

[xml]p:user-details-ref=<reference>[/xml]

Warning: Even though using p-namespace simplifies the configuration, it can create clashes with properties that end with “Ref”. General recommendation is that you need to put appropriate guidelines in place so as to avoid producing XML documents that contain a mix of such configurations at the same time.

Page Visitors: 3744

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 *