Spring Book – Chapter 5 – Application Configuration – Simplified

c-namespace

The c-namespace, newly introduced in Spring 3.1, allows usage of in line attributes for configuring the constructor arguments. The c-namespace uses the same conventions as the p-namespace (trailing -ref for bean references) for setting the constructor arguments by their names. Similar to p-namespace, it also has to be declared in the XML configuration even though additional schemaLocation entry for this is not required.

Listing 5-18. Namespace declaration for using c-namespace in your XML configuration

[xml]<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:c="http://www.springframework.org/schema/c"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!–bean configurations –>

</beans>[/xml]

Listing 5-19. Specifying a value as a constructor attribute to a bean without using c-namespace

[xml]<bean id="UserAccount" class="com.mybook.model.UserAccount">

<constructor-arg value="My User Account"/>

</bean>[/xml]

Listing 5-20. Specifying a value as a constructor attribute to a bean using c-namespace

[xml]<bean id="UserAccount" c: userAccountName="My User Account"/>[/xml]

Listing 5-21. Specifying a reference as a constructor attribute to a bean without using c-namespace

[xml]<bean id="UserAccount" class="com.mybook.model.UserAccount">

<constructor-arg ref="userDetails"/>

<constructor-arg value="My User Account"/>

</bean>

<bean id=" userDetails">

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

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

</bean>[/xml]

Listing 5-22. Specifying a reference as a constructor attribute to a bean using c-namespace

[xml]<bean id="UserAccount" class="com.mybook.model.UserAccount" c:userDetails-ref="userDetails" c: userAccountName="My User Account"/>

<bean id="userDetails" p:firstName="Isaac" p:lastName="Newton" />

</bean>[/xml]

If constructor argument names are not available (bean initialization of classes in third party jars), you have a provision of using argument index. Unless one really needs it, using name notation through-out your configuration is recommended.

Listing 5-23. Using constructor argiment index to initlaize beans using c-namespace

[xml]<bean id="UserAccount" class="com.mybook.model.UserAccount" c:_0-ref="userDetails" c:_1="My User Account"/>

<bean id="userDetails" p:firstName="Isaac" p:lastName="Newton" />

</bean>[/xml]

util-namespace

The util tags deal with common utility configuration issues such as configuring collections, referencing constants, etc. It is one of the built-in namespaces available in Spring. To use the tags in the util schema, you need to declare it at the top of your Spring XML configuration file. Unlike c-namespace and p-namespace, util-namespace should also have entry in the schemaLocation attribute. Listing 5-24 shows util-namespace declaration in the XML configuration.

Listing 5-24. util-namespace declaration in XML

[xml]<?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:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<!– Other bean definitions here –>

</beans>[/xml]

Page Visitors: 3752

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 *