Prototype Scope
Spring Inversion of Control (IoC ) also known as
Dependency Injection (DI ) is a process by which
objects define their dependencies with collaborating objects.
Prototype scoped bean permits a single bean definition to any number of
object instances per Spring IoC container.
package com.bethecoder.tutorials.spring3.basic;
public class SimpleBean {
private String simpleProp;
public String getSimpleProp () {
return simpleProp;
}
public void setSimpleProp ( String simpleProp ) {
this .simpleProp = simpleProp;
}
}
package com.bethecoder.tutorials.spring3.tests.scope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bethecoder.tutorials.spring3.basic.SimpleBean;
public class PrototypeTests {
/**
* @param args
*/
public static void main ( String [] args ) {
System.out.println ( "Initializing ApplicationContext" ) ;
ApplicationContext factory = new ClassPathXmlApplicationContext ( "prototype.xml" ) ;
System.out.println ( "ApplicationContext Initialized" ) ;
System.out.println ( "Accessing first bean" ) ;
SimpleBean first = ( SimpleBean ) factory.getBean ( "first" ) ;
System.out.println ( first ) ;
first = ( SimpleBean ) factory.getBean ( "first" ) ;
System.out.println ( first ) ;
first = ( SimpleBean ) factory.getBean ( "first" ) ;
System.out.println ( first ) ;
}
}
It gives the following output,
Initializing ApplicationContext
ApplicationContext Initialized
Accessing first bean
com.bethecoder.tutorials.spring3.basic.SimpleBean@12bcd4b
com.bethecoder.tutorials.spring3.basic.SimpleBean@b307f0
com.bethecoder.tutorials.spring3.basic.SimpleBean@1ce3fc5