问题引入
在springIOC容器的加载中,如果我们想让bean在我们需要的时候才加载,很明显会让spring启动的时间变快
怎么办
<bean name="A" class="com.wuhulala.studySpring.A" lazy-init="true"></bean>
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
System.out.println("-------------- 初始化 完成 --------------------");
A a = context.getBean(A.class);
System.out.println(a.getB());
-------------- 初始化 完成 --------------------
19:06:03,886 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory (221) - Creating shared instance of singleton bean 'A'
19:06:03,886 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory (448) - Creating instance of bean 'A'
19:06:03,886 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory (251) - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
19:06:03,887 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory (529) - Eagerly caching bean 'A' to allow for resolving potential circular references
19:06:03,892 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory (251) - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
19:06:03,892 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory (251) - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
19:06:03,895 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory (484) - Finished creating instance of bean 'A'
null
由上可知,我们在只需要在bean配置的时候设置lazy-init属性为true就好了。
同样适用java注解也可以
@Lazy
@Component
public class A {
}
那么我们想问了
真么神奇的东西 在spring内部是怎么实现的呢。
其实就是在bean实例化的时候判断bean 的lazyInit属性
public void preInstantiateSingletons() throws BeansException {
if(this.logger.isDebugEnabled()) {
this.logger.debug("Pre-instantiating singletons in " + this);
}
ArrayList beanNames = new ArrayList(this.beanDefinitionNames);
Iterator var2 = beanNames.iterator();
while(true) {
while(true) {
String beanName;
RootBeanDefinition singletonInstance;
do {
do {
do {
if(!var2.hasNext()) {
var2 = beanNames.iterator();
while(var2.hasNext()) {
beanName = (String)var2.next();
Object singletonInstance1 = this.getSingleton(beanName);
if(singletonInstance1 instanceof SmartInitializingSingleton) {
final SmartInitializingSingleton smartSingleton1 = (SmartInitializingSingleton)singletonInstance1;
if(System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
smartSingleton1.afterSingletonsInstantiated();
return null;
}
}, this.getAccessControlContext());
} else {
smartSingleton1.afterSingletonsInstantiated();
}
}
}
return;
}
beanName = (String)var2.next();
singletonInstance = this.getMergedLocalBeanDefinition(beanName);
} while(singletonInstance.isAbstract());
} while(!singletonInstance.isSingleton());
} while(singletonInstance.isLazyInit());//就是这里 中间的核心代码是依赖注入
if(this.isFactoryBean(beanName)) {
final FactoryBean smartSingleton = (FactoryBean)this.getBean("&" + beanName);
boolean isEagerInit;
if(System.getSecurityManager() != null && smartSingleton instanceof SmartFactoryBean) {
isEagerInit = ((Boolean)AccessController.doPrivileged(new PrivilegedAction() {
public Boolean run() {
return Boolean.valueOf(((SmartFactoryBean)smartSingleton).isEagerInit());
}
}, this.getAccessControlContext())).booleanValue();
} else {
isEagerInit = smartSingleton instanceof SmartFactoryBean && ((SmartFactoryBean)smartSingleton).isEagerInit();
}
if(isEagerInit) {
this.getBean(beanName);
}
} else {
this.getBean(beanName);
}
}
}
}