Spring Book – Chapter 5 – Application Configuration – Simplified

Inheritance without abstract

In the following example, “parentEmployee” bean can be instantiated and used like a normal bean. The property “address” is inherited, property “name” is added new and property “designation” is overridden in the “childEmployee” bean. Here “parentEmployee” indirectly becomes the default bean which can be inherited and appropriately changed as required.

[xml]<bean id="parentEmployee" class="com.mybook.sample.Employee">

<property name="address" value="Dubai"/>

<property name="designation" value="STE"/>

</bean>

<bean id="childEmployee" parent="parentEmployee">

<property name="name" value="john"/>

<property name="designation" value="SSE"/>

</bean>[/xml]

Pure inheritance template

With the following template, the parent bean is not necessary to define class attribute; you may just need a common property for sharing. In this case, the “parentEmployee” bean is a pure bean template, to share its “address” and “designation” property to its child beans if overridden.

[xml]<bean id="parentEmployee" abstract="true">

<property name="address" value="Dubai"/>

<property name="designation" value="STE"/>

</bean>

<bean id="childEmployee" parent="parentEmployee">

<property name="name" value="john"/>

<property name="designation" value="SSE"/>

</bean>[/xml]

Inner Beans

If a bean is used only for one particular property or as one of the attributes in the constructor, it’s better to declare it as an inner bean. Scoping a bean’s definition within another bean in such cases is good way to configure beans by using XML. Using this method is more manageable as certain beans which only makes sense inside a particular bean can be declared inside the parent bean and it avoids diluting the bean namespace.

The inner bean is supported both in setter injection (property) and constructor injection (constructor-arg). Inner Beans are like anonymous beans, where they are created and used on the fly. If you don’t want the actual bean instance to be accessible without a wrapper bean, then there is no problem with using inner beans. If beans are not given an id, its called as anonymous bean. Another important point about inner beans is that they are always scoped as prototype; the singleton flag is always ignored. Inner bean are prototypes, but if outer bean is singleton so the inner bean is created on outer bean initialization, which de facto makes them behave like singleton.

XML Example

Consider following Java classes shown in Listing 5-1.

Listing 5-1. Java classes used to explain the concept of inner beans

[java]public class UserAccount {

//properties

private UserDetails userDetails;

private AccountDetails accountDetails;

private String userAccountName;

//constructor

public UserAccount(UserDetails userDetails){

this.userDetails = userDetails;

}

//constructor

public UserAccount(String userAccountName){

this. userAccountName = userAccountName;

}

//constructor

public UserAccount(UserDetails userDetails, String userAccountName){

this.userDetails = userDetails;

this. userAccountName = userAccountName;

}

//…

}[/java]
[java]public class UserDetails {

private String firstName;

private String lastName;

private DepartmentDetails departmentDetails;

//…

}[/java]
[java]public class AccountDetails {

private String number;

private String branch;

//…

}[/java]
[java]public class DepartmentDetails {

private String name;

private String section;

//…

}[/java]

Listing 5-2. XML configuration without using inner beans (XML clumsier)

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

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

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

</bean>

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

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

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

</bean>

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

<property name="number" value="123456" />

<property name="branch" value="cochin" />

</bean>[/xml]

In the preceding XML configuration, the “UserDetails” and “AccountDetails” beans are configured the same as for normal beans, and because of this, these beans can be accessed by any other configured bean in this XML configuration. We can avoid this by scoping these beans inside the main “UserAccount” bean as inner beans. This makes the XML more readable and doesn’t dilute the bean namespace. This is one way by which the bean configuration can be simplified, as shown in Listing 5-3 and Listing 5-4:

Listing 5-3. Usage of inner beans to simplify XML configuration in setter injection

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

<property name="userDetails">

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

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

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

</bean>

</property>

<property name="accountDetails">

<bean class="com.mybook.model.AccountDetails">

<property name="number" value="123456" />

<property name="branch" value="cochin" />

</bean>

</property>

</bean>[/xml]

Listing 5-4. Usage of inner beans in constructor injection and setter injection

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

<constructor-arg>

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

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

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

</bean>

</constructor-arg >

<property name="accountDetails">

<bean class="com.mybook.model.AccountDetails">

<property name="number" value="123456" />

<property name="branch" value="cochin" />

</bean>

</property>

</bean>[/xml]

Note: When using inner beans, attribute id/name in the XML configuration is not required. Even if specified, Spring container completely ignores it.

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 *