Create Spring Beans using Factory Methods

Get 50% Off the Upcoming REST With Spring Classes over on Baeldung until the 15th of October.

The advantage of using the spring framework is the efficient beans management by the IOC container. We never create an instance in the code instead spring creates and manages them. In the certain scenarios, the demand is to use the factory methods for creating the instances of an particular object due to the restriction imposed by that object for not instantiating directly by using the constructors. This requirement is very common when we are using the service locator pattern create and manage the objects. Spring provides options to inform the IOC container to use the factory methods for creating the instances. This tutorial provides simple examples to use the factory methods for creating the bean instance.

There are two types of factory methods:

  1. Static factory method  : These are defined as the static methods and always have the single object instance for that class. Note that the factory method should be static, otherwise you would get an exception while creating the instance.
  2. Instance factory method: These objects are created by using the bean instance.

also read:

1. Create Factory Class

The below class defines the list of factory methods. First one is static factory method and the next two methods are instance factory methods. Before creating the factory class, also create the Java beans which needs to be act as the spring beans for our example.


public interface Transport {
	public void getTransport();


public class Car implements Transport{
	public void getTransport(){
		System.out.println("Transport type is Car");


public class Bus implements Transport {
	public void getTransport() {
		System.out.println("Transport type is Car");


public class SpringService {
	private static SpringService service = new SpringService();

	//Static factory method
	public static SpringService createService(){
		return service;

	//Instance factory methods
	public Transport createCarInstance(){
		return new Car();

	public Transport createBusInstance(){
		return new Bus();


2. Configure Factory Methods in Spring Configuration

If you look at the below code snippet, first I declare factory method for creating the instance of SpringService. After that using the SpringService beans, I declare instance factory methods. The below are the important attributes for creating the factory methods.

  • factory-method: This is the key attribute for mentioning the factory methods in the class.
  • factory-bean: This is required when we need instance factory methods.


<beans xmlns=""

	<context:component-scan base-package=""/>
	<bean id="springServiceStaticFactory" factory-method="createService"/>
	<bean id="springServiceCarInstance" factory-bean="springServiceStaticFactory" factory-method="createCarInstance"/>
	<bean id="springServiceBusInstance" factory-bean="springServiceStaticFactory" factory-method="createBusInstance"/>

3. Create Context Loader

Once we are ready with all the files, lets create the test application by loading the ApplicationContext. If you run the below example, it will create three beans and invoke them. I have used ClassPathXmlApplicationContext for loading the spring configuration file.



public class SpringContextLoader {
	public static void main (String args[]){
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("javabeat/net/core/ioc/spring-beans-ioc.xml");
		Transport transportCar = (Transport)applicationContext.getBean("springServiceCarInstance");
		Transport transportBus = (Transport)applicationContext.getBean("springServiceBusInstance");

I hope this example would help you to understand how to use the factory methods with spring configuration files.

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!