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
springframework作用* 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不起作⽤。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。