spring 加载过程中lazy-init 的使用与源码

问题引入

在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);
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值