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
1 2 3 4 5 6 7 8 9 10 11 |
public class LoyaltyServiceExample{ //…properties public LoyaltyService getInstance(){ } //..other methods } |
1 2 3 |
<bean id="loyaltyServiceExample’ class="com.mybook.loyalty.LoyaltyServiceExample"/> <bean id="loyaltyService" factory-bean="loyaltyServiceExample’" factory-method="getInstance"/> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class LoyaltyServiceFactory implements FactoryBean<LoyaltyService>{ //… public LoyaltyService getObject() throws Exception{ return loyaltyService; } public Class(?) getObjectType(){ return LoyaltyService.class; } public Boolean isSingleton(){ return true; } //… } |
1 |
<bean id="loyaltyService" class="com.mybook.loyalty.LoyaltyService"/> |
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
1 |
ApplicationContext context = new ClassPathXmlApplicationContext("com/mybook/loyalty/application-context.xml"); |
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
1 |
ApplicationContext context = new ClassPathXmlApplicationContext("com/mybook/loyalty/application-context.xml"," com/mybook/loyalty/infrastructure-context.xml"); |
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
1 |
ApplicationContext context = new FileSystemXmlApplicationContext ("./application-context.xml"," ./infrastructure-context.xml"); |
Page Visitors: 4349

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
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.
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