关于延迟加载的问题,有次和大神讨论他会不会直接或间接影响其他类。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注解,源码分析和总结的更多相关文章

  1. Spring Boot 揭秘与实战 源码分析 - 工作原理剖析

    文章目录 1. EnableAutoConfiguration 帮助我们做了什么 2. 配置参数类 – FreeMarkerProperties 3. 自动配置类 – FreeMarkerAutoCo ...

  2. Spring Boot 揭秘与实战 源码分析 - 开箱即用,内藏玄机

    文章目录 1. 开箱即用,内藏玄机 2. 总结 3. 源代码 Spring Boot提供了很多”开箱即用“的依赖模块,那么,Spring Boot 如何巧妙的做到开箱即用,自动配置的呢? 开箱即用,内 ...

  3. Spring Environment(二)源码分析

    Spring Environment(二)源码分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Envi ...

  4. Spring Security(四) —— 核心过滤器源码分析

    摘要: 原创出处 https://www.cnkirito.moe/spring-security-4/ 「老徐」欢迎转载,保留摘要,谢谢! 4 过滤器详解 前面的部分,我们关注了Spring Sec ...

  5. Spring Cloud Eureka服务注册源码分析

    Eureka是怎么work的 那eureka client如何将本地服务的注册信息发送到远端的注册服务器eureka server上.通过下面的源码分析,看出Eureka Client的定时任务调用E ...

  6. Spring Cloud学习 之 Spring Cloud Ribbon(负载均衡器源码分析)

    文章目录 AbstractLoadBalancer: BaseLoadBalancer: DynamicServerListLoadBalancer: ServerList: ServerListUp ...

  7. Mybatis整合Spring实现事务管理的源码分析

    一:前言 没有完整看完,但是看到了一些关键的地方,这里做个记录,过程会有点乱,以后逐渐补充最终归档为完整流程:相信看过框架源码的都知道过程中无法完全确定是怎样的流程,毕竟不可能全部都去测试一遍 ,但是 ...

  8. spring mvc 启动过程及源码分析

    由于公司开源框架选用的spring+spring mvc + mybatis.使用这些框架,网上都有现成的案例:需要那些配置文件.每种类型的配置文件的节点该如何书写等等.如果只是需要项目能够跑起来,只 ...

  9. 【spring源码学习】spring的远程调用实现源码分析

    [一]spring的远程调用提供的基础类 (1)org.springframework.remoting.support.RemotingSupport ===>spring提供实现的远程调用客 ...

  10. Spring之SpringMVC的RequestToViewNameTranslator(源码)分析

    前言 SpringMVC如果在处理业务的过程中发生了异常,这个时候是没有一个完整的ModelAndView对象返回的,它应该是怎么样处理呢?或者说应该怎么去获取一个视图然后去展示呢.下面就是要讲的Re ...

随机推荐

  1. ubuntu server usb安装盘制作问题

    本来服务器上装的是windows server 2003,开多个虚拟机装linux来用,但发现不管是vmware还是virtualbox,总是有内存泄漏,大约2个星期左右16G内存就全没了,任务管理器 ...

  2. 38、EST序列拼接流程

    转载:http://fhqdddddd.blog.163.com/blog/static/18699154201241014835362/ http://blog.sina.com.cn/s/blog ...

  3. Elasticsearch集群使用ik分词器

    IK分词插件的安装 ES集群环境 VMWare下三台虚拟机Ubuntu 14.04.2 LTS JDK 1.8.0_66 Elasticsearch 2.3.1 elasticsearch-jdbc- ...

  4. C# 字符串转JSON

    一.简单小结 C# 中 String 转 JSON var items = JsonConvert.DeserializeObject<class>(stringJSON); 这里的 cl ...

  5. SpringBoot应用篇(一):自定义starter

    一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...

  6. 三层登录——C#版

    前言 前期了解三层架构主要是由UI层.BLL层和DAL层三部分构成.看到大牛们都采用三层的思想实现了登录,本菜鸟暗暗地站在了他们的肩膀上. 自己理解 对于三层自己的理解是:就像我们对一个大型的公司去找 ...

  7. 消耗战——dp+虚树

    题目 [题目描述] 在一场战争中,战场由 $n$ 个岛屿和 $n-1$ 个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为 $1$ 的岛屿,而且他们已经没有足够多 ...

  8. [Xcode 实际操作]五、使用表格-(7)UITableView单元格间隔背景色

    目录:[Swift]Xcode实际操作 本文将演示如何给表格设置间隔的背景颜色. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先 ...

  9. MCP|WJ|Identification of candidate plasma protein biomarkers for cervical cancer using the multiplex proximity extension assay(利用多重邻位延伸分析技术进行宫颈癌血浆蛋白候选生物标记物的鉴定研究)

    文献名:Identification of candidate plasma protein biomarkers for cervical cancer using the multiplex pr ...

  10. P3369 【模板】普通平衡树

    纯板子.... 题意: 插入 xx 数 删除 xx 数(若有多个相同的数,因只删除一个) 查询 xx 数的排名(排名定义为比当前数小的数的个数 +1+1 .若有多个相同的数,因输出最小的排名) 查询排 ...