spring注解之@Lazy
今天主要从以下几方面来介绍一下@Lazy注解
@Lazy注解是什么
@Lazy注解怎么使用
1,@Lazy注解是什么
@Lazy注解用于标识bean是否需要延迟加载,源码如下:
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lazy {
/**
* Whether lazy initialization should occur.
*/
boolean value() default true;
}
只有一个参数,默认是true,也就是说只要加了这个注解就会延迟加载
2,@Lazy注解怎么使用
没加注解之前主要容器启动就会实例化bean,如下:
AnnotationConfigApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(MainConfig.class);
创建user实例
而加上@Lazy注解则必须在第一次调用的时候才会加载如下:
/**
* 定义一个bean对象
* @return
*/
@Scope
@Lazy
@Bean(value="user0",name="user0",initMethod="initUser",destroyMethod="destroyUser")
public User getUser(){
System.out.println("创建user实例");
return new User("张三",26);
}
AnnotationConfigApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(MainConfig.class);
User bean2 = applicationContext2.getBean(User.class);
创建user实例
实例1 === User [userName=张三, age=26]
@Lazy注解注解的作用主要是减少springIOC容器启动的加载时间
spring注解之@Lazy的更多相关文章
- Spring注解之@Lazy注解
@Lazy用于指定该Bean是否取消预初始化.主要用于修饰Spring Bean类,用于指定该Bean的预初始化行为, 使用该Annotation时可以指定一个boolean型的value属性,该属性 ...
- Spring注解之@Lazy注解,源码分析和总结
一 关于延迟加载的问题,有次和大神讨论他会不会直接或间接影响其他类.spring的好处就是文档都在代码里,网上百度大多是无用功. 不如,直接看源码.所以把当时源码分析的思路丢上来一波. 二 源码分析 ...
- 【Spring注解驱动开发】使用@Lazy注解实现懒加载
写在前面 Spring在启动时,默认会将单实例bean进行实例化,并加载到Spring容器中.也就是说,单实例bean默认在Spring容器启动的时候创建对象,并将对象加载到Spring容器中.如果我 ...
- spring注解大全
出自http://www.cnblogs.com/xiaoxi/p/5935009.html 1.@Autowired @Autowired顾名思义,就是自动装配,其作用是为了消除代码Java代码里面 ...
- spring注解简单记录
@Autowired 自动匹配,按类型 @qualifiter("beanname") 当有多个bean匹配时,可指定bean名称 @Resource byname优先匹配,然后b ...
- Spring注解 系列之Spring常用注解总结
参考:Spring系列之Spring常用注解总结 (1) Resource 默认是byName的方式进行bean配置,@AutoWired默认是按照byType的方式进行装配bean的:(2)Comp ...
- Spring 注解驱动(一)基本使用规则
Spring 注解驱动(一)基本使用规则 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.基本使用 @Configur ...
- Spring 注解原理(三)@Qualifier @Value
Spring 注解原理(三)@Qualifier @Value Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.Aut ...
- Spring注解 @Configuration
Spring注解 @Configuration 一.@Configuration的作用 二.@Configuration的Spring容器启动方式 三.不加@Configuration的@Bean的解 ...
随机推荐
- 【Jmeter自学】常见错误类型(九)
==================================================================================================== ...
- 《C++数据结构-快速拾遗》 基础常识
1.命名空间函数 namespace wjy { void print() { cout<<"; } int load(int num) { return num; } } us ...
- HashMap扩容机制
1.什么是resize: resize就是重新计算容量:当我们不断的向HashMap对象里不停的添加元素时,HashMap对象内部的数组就会出现无法装载更多的元素,这是对象就需要扩大数组的长度,以便能 ...
- [Unity优化]UI优化(一):RaycastTarget
参考链接: http://www.manew.com/thread-100366-1-1.html https://www.jianshu.com/p/3082ebf8a342 https://blo ...
- 获取get请求后面的参数
var str = "www.baidu.com?id=1&name=zhangsan"; var data = str.split("?"); con ...
- SLD Related Gateway Serivces Unavaliable
SAP NW 7.4 default switched on the ACL (access control list) in gateway service, so only local acces ...
- 获取手机浏览器IP的函数
function GetIP() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { //如果变量是非空或非零的值,则 empty()返回 FALSE. $IP = ...
- 34.scrapy解决爬虫翻页问题
这里主要解决的问题: 1.翻页需要找到页面中加载的两个参数. '__VIEWSTATE': '{}'.format(response.meta['data']['__VIEWSTATE']), '__ ...
- 解决oracle导入未分配表空间的问题
select 'alter table ' || t.TABLE_NAME || ' allocate extent;' from user_tables t order by t.TABLE_NAM ...
- ChannelSftp 远程下载目录
ChannelSftp 并不直接支持远程下载目录, 直接下载, 出现 : not supported to get directory ... 需要自己实现, 我的实现如下: /** * @param ...