Prototype Singleton : Singleton « Spring « Java Tutorial






File: context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   <bean id="primeIterator"
      class="PrimeIterator" singleton="false">
      <property name="start" value="1000"/>
   </bean>
</beans>

File: Main.java

import java.util.Properties;

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

public class Main {

  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory =
      new XmlBeanFactory(
         new ClassPathResource(
            "context.xml"
         )
      );

   PrimeIterator primeNumber1 = (PrimeIterator)beanFactory.getBean("primeIterator");
   PrimeIterator primeNumber2 = (PrimeIterator)beanFactory.getBean("primeIterator");

   System.out.println(primeNumber1.getNextPrime());
   System.out.println(primeNumber2.getNextPrime());
 }
}
class PrimeIterator {
  int start = -1;
   public int getNextPrime() {
       return start;
  }
  public int getStart() {
    return start;
  }
  public void setStart(int start) {
    this.start = start;
  }

}
  Download:  Spring-PrototypeSingleton.zip( 2,598 k)








28.11.Singleton
28.11.1.Prototype Singleton
28.11.2.BeanFactory PreInstantiate Singletons Integration
28.11.3.Spring Prototype
28.11.4.Singleton Scope And Prototype Scope