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 ...
随机推荐
- 三 Flask web开发快速入门
1:会话: from flask import Flask, url_for, request, render_template, session from werkzeug.utils import ...
- http请求中的get和post的区别
1.标准答案 GET在浏览器回退时是无害的,而POST会再次提交请求. GET产生的URL地址可以被Bookmark,而POST不可以. GET请求会被浏览器主动cache,而POST不会,除非手动设 ...
- eclipse中使用Maven插件报错:-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
步骤: 1.添加M2_HOME的环境变量 2.Preference->Java->Installed JREs->Edit 选择一个jdk 3.添加 -Dmaven.multiMod ...
- 利用包管理器安装Node.JS
步骤1:用curl获取源代码在我们用卷曲获取源代码之前,我们必须先升级操作系统,然后用卷发命令获取NodeSource添加到本地仓库. root@ubuntu-15:~#apt-get update安 ...
- 33、VCF格式
转载:http://blog.sina.com.cn/s/blog_7110867f0101njf5.html http://www.cnblogs.com/liuhui0622/p/6246111. ...
- iOS开发,在main thread以外的thread更新UI
如果需要在异步任务(Async Task)中更新UI,若直接设置UI,会导致程序崩溃. 例如,在异步block中去更改UI: NSOperationQueue *queue=[[NSOperation ...
- 阶段3-团队合作\项目-网络安全传输系统\sprint3-账号管理子系统设计\第2课-账号管理子系统设计
账号管理子系统的设计 客户端需要登录到服务器,在服务器去查询数据库,进行验证该用户. 打开client.c文件 编译之 把它复制到开发板里面去 这个程序是在本地数据库测试的!!!!!!!!!!!!!! ...
- Robots.txt在项目中的运用
在开发公司一个项目的过程中,有这样一个需求 该网站上面有一个search功能,可以search该网站上的任何包括特定内容的网页 现在有一个需求,就是针对几个特定的页面,我们希望网站上的search功能 ...
- 阿里云 Ubuntu 服务器 安装 pymssql
我是按照官方的说明文档: http://www.pymssql.org/en/stable/intro.html#getting-started 一步一步操作的,但是没有成功,在网上搜索了很多信息,最 ...
- Tomcat之NIO 启动与应用分析
概述 从入门Web开始一直在使用Tomcat,随着对网络相关的知识的进一步了解,觉得越有必有去阅读一下常用的开源服务器的整个工作流程,以及使用场景,对比几款服务器的优劣势.最终根据合适的业务场景进行优 ...