Spring注解之@Lazy注解,源码分析和总结
一
关于延迟加载的问题,有次和大神讨论他会不会直接或间接影响其他类。spring的好处就是文档都在代码里,网上百度大多是无用功。
不如,直接看源码。所以把当时源码分析的思路丢上来一波。
二 源码分析
/**
* Indicates whether a bean is to be lazily initialized.
* 用于bean的延迟加载
* <p>May be used on any class directly or indirectly annotated with {@link
* org.springframework.stereotype.Component @Component} or on methods annotated with
* {@link Bean @Bean}.
* 可以用于直接或间接使用的@Component类,或者@Bean方法
* <p>If this annotation is not present on a {@code @Component} or {@code @Bean} definition,
* eager initialization will occur. If present and set to {@code true}, the {@code @Bean} or
* {@code @Component} will not be initialized until referenced by another bean or explicitly
* retrieved from the enclosing {@link org.springframework.beans.factory.BeanFactory
* BeanFactory}. If present and set to {@code false}, the bean will be instantiated on
* startup by bean factories that perform eager initialization of singletons.
* 如果没有此注释则会直接加载。(也就是说启动的时候会按顺序注入spring容器)反之,则会在被另一个bean引用或显式引用前不会被初始化。
* <p>If Lazy is present on a {@link Configuration @Configuration} class, this
* indicates that all {@code @Bean} methods within that {@code @Configuration}
* should be lazily initialized. If {@code @Lazy} is present and false on a {@code @Bean}
* method within a {@code @Lazy}-annotated {@code @Configuration} class, this indicates
* overriding the 'default lazy' behavior and that the bean should be eagerly initialized.
* 如果@Configuration 上使用了Lazy,则@Configuration 中的所有都会被懒加载。若是没使用,则对项目中的方法进行正常加载,哪怕在其他地方写了Lazy。
* (因为spring默认注入顺序先执行@Configuration ,那么就算后面使用了Lazy实际上也已经在spring容器中了)
* <p>In addition to its role for component initialization, this annotation may also be placed
* on injection points marked with {@link org.springframework.beans.factory.annotation.Autowired}
* or {@link javax.inject.Inject}: In that context, it leads to the creation of a
* lazy-resolution proxy for all affected dependencies, as an alternative to using
* {@link org.springframework.beans.factory.ObjectFactory} or {@link javax.inject.Provider}.
* 除了作用于@Component组件或其@Bean初始化方法,也作用于Inject和Autowired。在这种情况下,它会导致创建一个所有受影响的依赖项的延迟解析代理,作为使用的替代方法
* (就是Autowired注释的bean会默认进行懒加载,除非他之前就被加载了,类似于@Configuration的情况)*/
@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;//也就是不用标签是false,用就是true,网上什么@Lazy(true)大多是无谓代码 }
三 总结
就是分两种情况作用于 配置和其相关方法等先加载的 ,作用于 Autowired等后加载的。
特点有两条
先加载的覆盖后加载的。直接的覆盖间接的。
第一条优先于第二条。
就是后加载的间接Bean若是在先加载的配置里被使用了,那么Lazy不起作用。
Spring注解之@Lazy注解,源码分析和总结的更多相关文章
- Spring Boot 揭秘与实战 源码分析 - 工作原理剖析
文章目录 1. EnableAutoConfiguration 帮助我们做了什么 2. 配置参数类 – FreeMarkerProperties 3. 自动配置类 – FreeMarkerAutoCo ...
- Spring Boot 揭秘与实战 源码分析 - 开箱即用,内藏玄机
文章目录 1. 开箱即用,内藏玄机 2. 总结 3. 源代码 Spring Boot提供了很多”开箱即用“的依赖模块,那么,Spring Boot 如何巧妙的做到开箱即用,自动配置的呢? 开箱即用,内 ...
- Spring Environment(二)源码分析
Spring Environment(二)源码分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Envi ...
- Spring Security(四) —— 核心过滤器源码分析
摘要: 原创出处 https://www.cnkirito.moe/spring-security-4/ 「老徐」欢迎转载,保留摘要,谢谢! 4 过滤器详解 前面的部分,我们关注了Spring Sec ...
- Spring Cloud Eureka服务注册源码分析
Eureka是怎么work的 那eureka client如何将本地服务的注册信息发送到远端的注册服务器eureka server上.通过下面的源码分析,看出Eureka Client的定时任务调用E ...
- Spring Cloud学习 之 Spring Cloud Ribbon(负载均衡器源码分析)
文章目录 AbstractLoadBalancer: BaseLoadBalancer: DynamicServerListLoadBalancer: ServerList: ServerListUp ...
- Mybatis整合Spring实现事务管理的源码分析
一:前言 没有完整看完,但是看到了一些关键的地方,这里做个记录,过程会有点乱,以后逐渐补充最终归档为完整流程:相信看过框架源码的都知道过程中无法完全确定是怎样的流程,毕竟不可能全部都去测试一遍 ,但是 ...
- spring mvc 启动过程及源码分析
由于公司开源框架选用的spring+spring mvc + mybatis.使用这些框架,网上都有现成的案例:需要那些配置文件.每种类型的配置文件的节点该如何书写等等.如果只是需要项目能够跑起来,只 ...
- 【spring源码学习】spring的远程调用实现源码分析
[一]spring的远程调用提供的基础类 (1)org.springframework.remoting.support.RemotingSupport ===>spring提供实现的远程调用客 ...
- Spring之SpringMVC的RequestToViewNameTranslator(源码)分析
前言 SpringMVC如果在处理业务的过程中发生了异常,这个时候是没有一个完整的ModelAndView对象返回的,它应该是怎么样处理呢?或者说应该怎么去获取一个视图然后去展示呢.下面就是要讲的Re ...
随机推荐
- shell入门-变量
shell变量分为系统变量和用户自定义变量 查看变量的命令 #env 系统变量 或者 #set 包括env和自定义变量和额外变量 使用变量的命令是 #echo $[变量] //// ...
- df查看分区使用情况
Linux df命令用于显示目前在Linux系统上的文件系统的磁盘使用情况统计.语法 df [选项]... [FILE]... -a, --all 包含所有的具有 0 Blocks 的文件系统 ...
- ubuntu安装配置ApachePhpMysql
1.安装之前先sudo源 sudo apt update 2.安装Apache2 sudo apt install apache2 3.更改默认目录: vi /etc/apache2/apache2. ...
- 我对PageRank的理解及R语言实现
PageRank,网页排名,又称网页级别.Google左侧排名或佩奇排名,是一种由搜索引擎根据网页之间相互的超链接计算的技术,而作为网页排名的要素之一,以Google公司创办人拉里·佩奇(Larry ...
- 8、非root权限下安装perl以及perl模块
转载:http://www.cnblogs.com/nkwy2012/p/6418669.html 转载自http://www.zilhua.com 在本博客中,所有的软件安装都在服务器上,且无roo ...
- LEADTOOLS V19: 世界领先的图像处理开发工具包强势来袭
投递人 itwriter 发布于 2014-12-22 16:04 评论(0) 有214人阅读 原文链接 [收藏] « » LEAD 科技于 2014 年 12 月 11 日发布 LEA ...
- Entity Framework Code-First(9.8):DataAnnotations - Column Attribute
DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Defaul ...
- Sharepoint2013商务智能学习笔记之Performancepoint service 配置(九)
1)配置Performance Service服务 第一步,新建performance service.先在管理中心,系统设置区域点击管理服务器上的服务,确认Performance Service服务 ...
- Go语言——没有对象的面向对象编程
本文译自Steve Francia在OSCON 2014的一个PPT,原作请前往:https://spf13.com/presentation/go-for-object-oriented-progr ...
- MySQL 杂项
关于MySQL mysql连接时的--prompt 和 --delimiter 参数是用来设置什么的? 设置提示符和分隔符 mysql查看创建数据库时的字符集命令? SHOW CREATE DATAB ...