Spring Book – Chapter 4 – Configuration Styles

Factory Beans

Any class using Spring configuration can be made into a factory. Let’s take an example of a class as below which is a plain POJO class. With the power of Spring Framework, using XML configuration we can make this a class automatically implementing the factory pattern. This is shown as below:

Listing. Making a POJO a factory using Spring configuration using XML

A different way of achieving creation of factory pattern objects in your application is to make your objects implement the Spring-provided interface named FactoryBean. Spring Framework internally uses this approach of creating the factory pattern classes. The Spring Framework automatically detect beans implementing FactoryBean interface. Class declared in the Spring configuration as bean, automatically calls the getObject() method to create the object.

Listing. Usage of FactoryBean interface

The above line makes Spring to call the getObject() automatically to create the object and maintain in the Spring container.

Best Practice: Prefer setter injection over constructor injection. (See Chapter 3 for more information about these injection methods.) Constructor injection can ensure that a bean cannot be constructed in an invalid state, but setter injection is more flexible and manageable, especially when the class has multiple properties and some of them are optional.

It’s good to follow naming conventions while defining beans. Have descriptive IDs and give a header comment for each configuration file in your application.

Prefer IDs over names while defining a bean. Using IDs will not increase readability, but it can leverage the XML parser to validate the bean references.

Creating ApplicationContext

Using XML-based configuration, Spring can create the ApplicationContext but we will have to let the container know which XML files need to be loaded before we create the ApplicationContext. There are various ways by which to create the ApplicationContext in Spring Framework. The Spring can load the bean definition files from the classpath, from local file system and environment-relative resource path. The Spring Framework uses appropriate classes to achieve this. The power of Spring Framework comes as these ApplicationContext can be created in various environments like your JUnit test cases, web application and standalone java application with ease. They are explained in detail in the following sections.

ClassPathXmlApplicationContext

Creation of application context using the class ClassPathXmlApplicationContext is shown below:

Listing. Context creation using ClassPathXmlApplicationContext class

The file application-context.xml is in the classpath in the following location $CLASSPATH/ com/mybook/loyalty/application-context.xml. The application context can be configured from multiple files as follows:

Listing. Context creation using ClassPathXmlApplicationContext from multiple configuration files

FileSystemXmlApplicationContext

Creation of application context using the class FileSystemXmlApplicationContext is shown below:

Listing. Context creation using ClassPathXmlApplicationContext class

ApplicationContext context = new FileSystemXmlApplicationContext(“C:\\spring\\project\\application-context.xml”);

The file application-context.xml is in the operating system absolute path in the following location C:\spring\project\application-context.xml. Similar to ClassPathXmlApplicationContext, FileSystemXmlApplicationContext can be used to configure application context from multiple files as follows:

Listing. Context creation using FileSystemXmlApplicationContext from multiple configuration files

ApplicationContext context = new FileSystemXmlApplicationContext (“C:\\spring\\project\\application-context.xml”,” C:\\spring\\project\\infrastructure-context.xml”);

If the bean definition files are located relative to the JVM working directory which you would like to load in your application, it can be done as follows:

Listing. Context creation using FileSystemXmlApplicationContext from multiple configuration files which is located relative to JVM working directory

Page Visitors: 3600

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 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.

      Regards
      Tomcy John

Leave a Reply

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