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.
package com.bethecoder.tutorials.spring3.basic;
public class Foo {
public Bar createBar () {
return new Bar () ;
}
public static class Bar {
}
}
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 = ( Foo ) factory.getBean ( "foo" ) ;
System.out.println ( foo ) ;
Foo.Bar bar = ( Foo.Bar ) factory.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