In the ApplicationContext
container, when the container is started, all of the bean (singleton) will be created and injected dependency, which is regarded as a step IOC container during startup.
How to make a bean that is created again when needed, rather than a container load time?
In the profile bean
tag, the lazy-init
attribute to true
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
<bean name="not.lazy" class="com.foo.AnotherBean"/>
Thus, when applicationContext container starts, the Bean is not initialized, but when you need to use this time will create an instance of the class
There is another situation in which this is lazy loaded Bean, it was not a lazy loaded bean depends, then when the container is started, even if it is lazy loading, will be instantiated.
Global lazy loading
<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>
In the beans
configuration tag default-lazy-init="true"
attribute.