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:
- 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.
- 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
package javabeat.net.core.ioc; public interface Transport { public void getTransport(); }
Car.java
package javabeat.net.core.ioc; public class Car implements Transport{ public void getTransport(){ System.out.println("Transport type is Car"); } }
Bus.java
package javabeat.net.core.ioc; public class Bus implements Transport { public void getTransport() { System.out.println("Transport type is Car"); } }
SpringService.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(); } }
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
<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>
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
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(); } }
I hope this example would help you to understand how to use the factory methods with spring configuration files.