BeanFactory PreInstantiate Singletons Integration : 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="instantiationCounterClass"
      class="InstantiationCounterClass"/>
</beans>

File: Main.java

import java.util.concurrent.atomic.AtomicInteger;

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

public class Main {

  public static void main(String[] args) throws Exception {
    ConfigurableListableBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
        "context.xml"));
    System.out.println(InstantiationCounterClass.getNumberOfInstantiations());
    beanFactory.preInstantiateSingletons();
    System.out.println(InstantiationCounterClass.getNumberOfInstantiations());
  }
}

class InstantiationCounterClass {
  private static AtomicInteger instantiationCounter = new AtomicInteger(0);

  public static int getNumberOfInstantiations() {
    return instantiationCounter.get();
  }

  public InstantiationCounterClass() {
    instantiationCounter.incrementAndGet();
  }
}
  Download:  Spring-BeanFactoryPreInstantiateSingletonsIntegration.zip( 2,599 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