Use lazy-init. Tell the spring container whether to create objects in a lazy manner. The structure is loaded when it is used, and
lazy loading is not loaded when not in use : Value: true (lazy, load when it is actually called); default (lazy);
non-lazy loading : false (not lazy, just start the spring container Create object);
<bean id="test1" class="cn.java.ioc1.YelloMouseWolf" lazy-init="default" ></bean>
<bean id="startQuertz" lazy-init="false"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"></bean>
You can also set lazy loading and initialization methods for a group of beans:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"
default-lazy-init="true" default-init-method="init">
<bean id = "computer" class="com.zlc.test.Computer">
<constructor-arg index="0" ref="date"></constructor-arg>
</bean>
<bean id="date" class="java.util.Date">
</bean>
</beans>
Advantages and disadvantages of lazy loading and non-lazy loading:
Lazy loading : The object is created only when it is used (when the getBean() method is used), which saves resources, but is not conducive to early detection of errors.
Non-lazy loading : The object is created immediately when the container is started (when the spring container is created). Consume resources. Conducive to early detection of errors.
When scope="prototype" (multiple cases), the object is generated by lazy loading by default.
When scope="singleton" (singleton), the object is generated in a non-lazy way by default.