关于延迟加载的问题,有次和大神讨论他会不会直接或间接影响其他类。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. C#设计模式(7)——适配器模式

    一.概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 二.模型 三.代码实现 using System; /// 这里以 ...

  2. 性能分析工具 DotTrance

    1 本例子采用dotTrace 5.3 版本 运行dotTrace 5.3 .exe 选择独立应用程序 Standalone    Application 选择需要测试的应用程序exe的路径 点击啊开 ...

  3. shutdown-用于关闭/重启计算机

    Linux系统下的shutdown命令用于安全的关闭/重启计算机,它不仅可以方便的实现定时关机,还可以由用户决定关机时的相关参数.在执行shutdown命令时,系统会给每个终端(用户)发送一条屏显,提 ...

  4. 网络编程中阻塞和非阻塞socket的区别

    阻塞socket和非阻塞socket 建立连接阻塞方式下,connect首先发送SYN请求道服务器,当客户端收到服务器返回的SYN的确认时,则connect返回.否则的话一直阻塞.非阻塞方式,conn ...

  5. error C2039: “addTextureMesh”: 不是“pcl::visualization::PCLVisualizer”的成员

    error C2039: "addTextureMesh": 不是"pcl::visualization::PCLVisualizer"的成员 PCL 1.6 ...

  6. 16.Tomcat弱口令 && 后台getshell漏洞

    Tomcat7+ 弱口令 && 后台getshell漏洞 Tomcat版本:8.0 环境说明 Tomcat支持在后台部署war文件,可以直接将webshell部署到web目录下.其中, ...

  7. bat实现监测计算机无线连接,断网自动重启无线

    @echo off :Begin ping www.baidu.com if errorlevel 1 goto Reboot if errorlevel 0 goto Continue :Conti ...

  8. Note: Differentially Private Access Patterns for Searchable Symmetric Encryption

    The Core Issues and Ideas of This Paper Problem Baseline Searchable Symmetric Encryption (SSE) could ...

  9. Python中的try和except语句

    Python中是通过缩进来解析代码块的,要特别注意tab和空格符,两者不可以混用,通常情况下用四个空格来代替tab键 下面通过一个简单的例子来说明 提示用户输入工作的时间和每小时的时薪,如果超过40个 ...

  10. 2017-10-6 清北刷题冲刺班a.m

    1.角谷猜想 #include<iostream> #include<cstdio> #include<cstring> #define maxn 10010 us ...