JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Create Spring Beans using Factory Methods

November 8, 2013 by Krishna Srinivasan Leave a Comment

Build Your REST API with Spring - VIDEO Courses

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:

  • Spring Tutorials ( Collection for Spring reference documentations)
  • Spring Framework Interview Questions
  • Email Integration in Spring Framework
  • Introduction to Spring REST Services

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.

Transpost.java

[code lang=”java”]
package javabeat.net.core.ioc;

public interface Transport {
public void getTransport();
}
[/code]

Car.java

[code lang=”java”]
package javabeat.net.core.ioc;

public class Car implements Transport{
public void getTransport(){
System.out.println("Transport type is Car");
}
}
[/code]

Bus.java

[code lang=”java”]
package javabeat.net.core.ioc;

public class Bus implements Transport {
public void getTransport() {
System.out.println("Transport type is Car");
}
}
[/code]

SpringService.java

[code lang=”java”]

package javabeat.net.core.ioc;

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();
}

}
[/code]

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.

spring-beans-ioc.xml

[code lang=”xml”]
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:component-scan base-package="javabeat.net.core.ioc"/>
<bean id="springServiceStaticFactory" factory-method="createService"/>
<bean id="springServiceCarInstance" factory-bean="springServiceStaticFactory" factory-method="createCarInstance"/>
<bean id="springServiceBusInstance" factory-bean="springServiceStaticFactory" factory-method="createBusInstance"/>
</beans>
[/code]

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.

SpringContextLoader.java

[code lang=”java”]
package javabeat.net.core.ioc;

import org.springframework.context.support.ClassPathXmlApplicationContext;

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");
transportCar.getTransport();
Transport transportBus = (Transport)applicationContext.getBean("springServiceBusInstance");
transportBus.getTransport();
applicationContext.close();
}
}
[/code]

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

Filed Under: Spring Framework Tagged With: Spring Core

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved