Spring Book – Chapter 4 – Configuration Styles

In this Chapter, I will be explaining the various types of configuration styles that are available in Spring Framework to achieve Dependency Injection (DI). I will explain in detail the various configuration styles with working sample codes for clear understanding of each configuration styles. I will then give some best practices which can be followed to make your code maintainable and ways to avoid confusions and frustration to the developers as the application grows in size over a period of time.

Introduction to Spring Configuration Styles

Spring provides the following three ways of supplying metadata to the spring container to make the beans in your application ready to be used in your application as fully capable java objects:

  • XML
  • Annotations
  • Java

We can even mix and match the various ways of configurations in your application at will and spring doesn’t specify a particular way as best because it depends on the application that you are developing and the standards that your organization have already set. In the following sections, we will explore in detail the various configuration styles on which you could build your enterprise application.

History of Configuration Styles

Even though Spring allows developers to use any of the configuration styles currently available in your Spring application, it has supported various styles over a period of time, starting from the first version of Spring Framework, which was released in 2004. The history of configuration styles can be represented in tabular form as below:

Spring Version


Configuration Style


Spring 1.0 2004 XML

Figure 4-1. XML Configuration

Spring 2.5 2007 Annotation

Figure 4-2. Annotation Configuration

Spring 3.0 2009 Java

Figure 4-3. Java Configuration

XML-based configuration

XML based configuration style was the first style based on which Spring Framework began its journey in the framework business. As the framework evolved over a period of time various alternatives to XML based configuration came and the framework accepted one by one configuration style gracefully. Spring uses dependency injection (DI) to achieve simplification and testability. (See Chapter 3 for detailed information about what DI is and the different forms of DI and their use.) However, the XML configuration files are verbose and unwieldy. They can become hard to read and manage when you are working on a large project where many Spring beans are defined. Simple XML based dependency injection in Spring is done shown in Listing 4-1:

Listing 4-1. XML based Dependency Injection in Spring


Figure 4-4. XML based configuration lifecycle

Spring container loads the XML definitions according to the file locations supplied to the various ApplicationContext provided by Spring Framework. (The various ApplicationContext provided by Spring Framework are detailed in below section namely “Creating ApplicationContext”). Once this is done the BeanFactoryPostProcessor methods defined in the application are called. Soon after this, the actual bean instantiation and dependency injection happens. If BeanPostProcessor is defined, these are called as described in Chapter 3.

The following section provides an example of declaring/injecting various configurations using XML.

Injecting Scalar Values

Here in the below code snippet, “myString” and “32” are two scalar variables (member variables) in the class LoyaltyServiceImpl which need to be injected using the configuration.

Listing. Injection of Scalar variables in XML configuration

Corresponding Java code to the above XML configuration is:

Listing. Java Code for the above XML configuration

To show the example of bean configuration which requires injecting of lists and other collection types namely properties, Set and Map, I would like to use a class as below:

Listing. Class used to explain the injection of various types in XML configuration

Page Visitors: 2955

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 the Senior Technical Engineer 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 Principal Architect to facilitate incorporating Spring as Corporate Standard in the organization.

2 thoughts on “Spring Book – Chapter 4 – Configuration Styles

  1. Hi there,

    There is one mistake repeated several times here. The value of the propertyInteger is being set using “ref” instead of “value” and you may want to remove the spaces before and after some of the values/properties to avoid others new to the framework copying and pasting and getting tripped up.

    Apart from that, thanks for making the material available for free.

    1. Sure will do the necessary changes. I wrote the material using MS Word and after that when i copied, these spaces automatically came in.

      Thanks for pointing it out.

      Tomcy John

Leave a Reply

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