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

Factory Bean 

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 invoking an instance factory method on a bean instance.

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

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

public class Foo {
  public Bar createBar() {
    return new Bar();
  }
  
  public static class Bar {
  }
}
   

File Name  :  
com/bethecoder/tutorials/spring3/tests/factory/FactoryBean.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
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.Foo;

public class FactoryBean {

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

    Foo foo = (Foofactory.getBean("foo");
    System.out.println(foo);
    
    Foo.Bar bar  = (Foo.Barfactory.getBean("bar");
    System.out.println(bar);
  }

}
   

It gives the following output,
com.bethecoder.tutorials.spring3.basic.Foo@170888e
com.bethecoder.tutorials.spring3.basic.Foo$Bar@1e845c2


                    Print

   



Books Recommendation

 
 
bl  br