Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Instantiating beans using static factory and instance factory in Spring

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 454
    Comment on it

    The benefit of using the spring framework is the masterful beans management by the IOC container. Spring framework confers options to impart the IOC container to use the factory methods for creating the instances.
    Types of factory methods:
    1. Static factory method  : Static factory method is simply a static method that returns an instance of a class.
    2. Instance factory method: These objects are created by using the bean instance.

    Bellow example will help you to better understand the factory method in spring framework.
    Follow bellow steps :

    Step :1 Create Factory Classes

    Transpost.java

    package findnerd.core.ioc;
    public interface Transport {
    
        public void getTransport();
    
    }
    

    Car.java

    package findnerd.core.ioc;
    public class Car implements Transport{
    
        public void getTransport(){
    
            System.out.println("Transport type is Car");
    
        }
    
    }
    

    Bus.java

    package findnerd.core.ioc;
    public class Bus implements Transport {
    
        public void getTransport() {
    
            System.out.println("Transport type is Car");
    
        }
    
    }
    

    SpringService.java

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

    Step: 2 Factory Methods in Spring Configuration
    Create spring-beans-ioc.xml and put bellow code.

    <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="findnerd.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>
    

    Step: 3 Context Loader
    Create SpringContextLoader.java and put bellow code.

    package findnerd.core.ioc;
    
        import org.springframework.context.support.ClassPathXmlApplicationContext;
    
        public class SpringContextLoader {
            public static void main (String args[]){
                ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("findnerd/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 you will understand that how to use the factory methods with spring configuration files.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: