博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring之lazy-init
阅读量:4189 次
发布时间:2019-05-26

本文共 8490 字,大约阅读时间需要 28 分钟。

Spring 之lazy-init

为什么想讨论spring这个属性了,起因是自己在思考,spring容器管理的bean,是在spring容器启动完成后,所有的bean的都被实例化了吗?

先回答上面的问题,spring容器管理的bean.在spring容器启动完成后,都被实例化了。认证过程如下:

有三个类,StudyClient,StudyService,StudyDao.

StudyClient

package com.spring.study;import org.springframework.context.support.ClassPathXmlApplicationContext;public class StudyClient {    public static void main(String[] args){        ClassPathXmlApplicationContext classPathXmlApplicationContext =                new ClassPathXmlApplicationContext("classpath:spring.xml");    }}

StudyService

package com.spring.study;public class StudyService {    public StudyService(){        System.out.println("initialize study service");    }    private StudyDao studyDao;    public void setStudyDao(StudyDao studyDao){        this.studyDao = studyDao;    }    public void invokeStudyDao(){        studyDao.invoke();    }}

StudyDao

package com.spring.study;public class StudyDao {    public StudyDao(){        System.out.println("initialized study dao");    }    public void invoke(){        System.out.println("StudyDao was invoked");    }}

spring.xml的配置文件

<?xml version="1.0" encoding="UTF-8"?>

调用main方法,启动spring 容器,可以如下打印信息,如红色字体锁是,看到两个类被实例化了。因为两个类的构造函数被调用了。

14:26:26.139 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean ‘studyDao’

initialized study dao
14:26:26.149 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean ‘studyDao’ to allow for resolving potential circular references
14:26:26.150 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean ‘studyDao’
14:26:26.151 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘studyService’
14:26:26.151 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean ‘studyService’
initialize study service

接下来,我新增一个类,StudyLazyInit

package com.spring.study;

public class StudyLazyInit {

public StudyLazyInit(){    System.out.println("is StudyLazyInit init?");}public void init(){    System.out.println("StudyLazyInit is initialize");}

}

修改了spring.xml

启动main函数,启动spring容器,看打印日志

[org.springframework.context.support.DelegatingMessageSource@e320068]

14:46:08.108 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate ApplicationEventMulticaster with name ‘applicationEventMulticaster’: using default [org.springframework.context.event.SimpleApplicationEventMulticaster@34cd072c]
14:46:08.109 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@a67c67e: defining beans [studyService,studyDao,studyLazyInit]; root of factory hierarchy
14:46:08.110 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘studyService’
14:46:08.110 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean ‘studyService’
initialize study service
14:46:08.119 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean ‘studyService’ to allow for resolving potential circular references
14:46:08.120 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘studyDao’
14:46:08.120 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean ‘studyDao’
initialized study dao
14:46:08.121 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean ‘studyDao’ to allow for resolving potential circular references
14:46:08.121 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean ‘studyDao’
14:46:08.161 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean ‘studyService’
14:46:08.161 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean ‘studyDao’
14:46:08.162 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name ‘lifecycleProcessor’: using default [org.springframework.context.support.DefaultLifecycleProcessor@4d1b0d2a]
14:46:08.162 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean ‘lifecycleProcessor’
14:46:08.165 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key ‘spring.liveBeansView.mbeanDomain’ in any property source

Process finished with exit code 0

从上面的日志信息里面可以看到,StudyLazyInit没有被实体化,而是被预初始化了。原因就在Lazy-init = “true”. 如下是在spring.xml里面增加的配置内容

将Lazy-init 设置为false ,就可以在日志里面看StudyLazyInit被初始化了。

14:58:13.816 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@a67c67e: defining beans [studyService,studyDao,studyLazyInit]; root of factory hierarchy

14:58:13.817 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘studyService’
14:58:13.817 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean ‘studyService’
initialize study service
14:58:13.827 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean ‘studyService’ to allow for resolving potential circular references
14:58:13.828 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘studyDao’
14:58:13.828 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean ‘studyDao’
initialized study dao
14:58:13.829 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean ‘studyDao’ to allow for resolving potential circular references
14:58:13.830 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean ‘studyDao’
14:58:13.888 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean ‘studyService’
14:58:13.888 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean ‘studyDao’
14:58:13.888 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘studyLazyInit’
14:58:13.888 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean ‘studyLazyInit’
is StudyLazyInit init?
14:58:13.888 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean ‘studyLazyInit’ to allow for resolving potential circular references
14:58:13.888 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean ‘studyLazyInit’
14:58:13.889 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name ‘lifecycleProcessor’: using default [org.springframework.context.support.DefaultLifecycleProcessor@4d1b0d2a]
14:58:13.889 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean ‘lifecycleProcessor’
14:58:13.892 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key ‘spring.liveBeansView.mbeanDomain’ in any property source

Process finished with exit code 0

如果Lazy-init 被设置成true,那么bean何时实例化了,答案是使用bean,向spring 容器索要bean的时候,即getBean(“beanName”).并且bean所以依赖的bean也被实例化(尽管其也是懒加载)和注入。

在验证,被依赖的类如果也是延迟加载的话,我们在getBean依赖类时候,也会被实例化。但是这是有条件的。条件就是必须是set注入,不是autowire注入。

稍微解释一下。假设A 是依赖类, B被A依赖,那么B就是被依赖类。

A 属性B ,引入属性B有三种方式,构造函数,set ,autowire.

其中构造函数,和 set注入,不管B是否延迟加载,当我在spring容器种getBean()获取A的时候,B就会被实例化,而 用autowire 就不会实例化。

转载地址:http://xusoi.baihongyu.com/

你可能感兴趣的文章
Tomcat性能调整优化
查看>>
利用SQL Server 2005减轻生产服务器优化负荷
查看>>
优化MYSQL服务器
查看>>
Exchange磁盘性能优化
查看>>
Apusic应用服务器的性能调节_JVM优化
查看>>
Apache重负荷服务器应如何优化?
查看>>
Windows NT/2000服务器优化
查看>>
Windows 2003系统优化技巧
查看>>
Linux NFS服务器性能优化
查看>>
FREEBSD升级及优化全攻略
查看>>
RISC架构服务器开源运动将促使市场需求提升
查看>>
IT治理的成功要诀
查看>>
中化CIO彭劲松:IT治理让我明明白白做事
查看>>
中国惠普公司企业计算及专业服务集团卫东:IT治理最重要就是保证技术与业务有效结合
查看>>
【MVP】 Wenzhong Huang 北大硕士,微软MVP,微软嵌入式讲师,MCSE
查看>>
解析ERP部署的三角模型
查看>>
百感交集:一个IT人应该如何面对失业?
查看>>
服装经营中关于直销、加盟、代理和联营的区别
查看>>
盯上好男人 服装业B2C暗战
查看>>
局域网内部管理行为应该如何控制?
查看>>