File: context.xml
"http://www.springframework.org/dtd/spring-beans.dtd">
class="InstantiationCounterClass"/>
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();
}
}