Prototype Singleton : Singleton « Spring « Java






Prototype Singleton

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;
  }

}




           
       








Spring-PrototypeSingleton.zip( 2,598 k)

Related examples in the same category

1.BeanFactory PreInstantiate Singletons Integration