今天在看代码的时候,发现了一个新的注解----@Lazy,于是记录下了。
1.顾名思义,Lazy就是延迟加载的意思。
@Lazy注解用于类上面,表示该类在需要用到的时候,才会创建,不需要的时候,不会创建出来。看下列代码
private AuthenticationManager authenticationManager;
private WebSecurityProperties webSecurityProperties;
private CaptchaService captchaService;
//@lazy=true代表延时,需要用到的时候再创建该对象
@Autowired
public AuthorizationServerConfigurer(@Lazy AuthenticationManager authenticationManager,
WebSecurityProperties webSecurityProperties,
CaptchaService captchaService) {
this.authenticationManager = authenticationManager;
this.webSecurityProperties = webSecurityProperties;
this.captchaService = captchaService;
}
这是我们在实际开发中经常使用的注入方式, AuthenticationManage类前面有个@Lazy注解,这意味着,在程序运行的时候,需要用到该类,才会创建对象,用不到就不会创建,这样对程序的性能起到了一定的优化作用。
2.@Lazy的默认值为true。
查看该注解的源代码,可以得知,默认true