tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > Core > Factory Method

Factory Method 

Spring Inversion of Control (IoC) also known as Dependency Injection (DI) is a process by which objects define their dependencies with collaborating objects. This example shows getting a DriverManager instance by invoking a static factory method

File Name  :  
/SpringCore001/conf/factory/factory_method.xml 

File Name  :  
com/bethecoder/tutorials/spring3/basic/DriverManager.java 
   
package com.bethecoder.tutorials.spring3.basic;

public class DriverManager {

  private static DriverManager clientService = new DriverManager();
  private DriverManager() {
  }

  public static DriverManager getInstance() {
    return clientService;
  }
}
   

File Name  :  
com/bethecoder/tutorials/spring3/tests/factory/FactoryMethod.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.spring3.tests.factory;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.bethecoder.tutorials.spring3.basic.DriverManager;

public class FactoryMethod {

  /**
   @param args
   */
  public static void main(String[] args) {
    XmlBeanFactory factory = new XmlBeanFactory(
                new ClassPathResource("factory_method.xml"));

    DriverManager driverMgr1 = (DriverManagerfactory.getBean("driverMgr1");
    System.out.println(driverMgr1);
    
    DriverManager driverMgr2 = (DriverManagerfactory.getBean("driverMgr2");
    System.out.println(driverMgr2);
  }

}
   

It gives the following output,
com.bethecoder.tutorials.spring3.basic.DriverManager@c39a20
com.bethecoder.tutorials.spring3.basic.DriverManager@c39a20



 
  


  
bl  br