Spring中@Lazy注解的使用示例教程
Spring在應(yīng)用程序上下文啟動(dòng)時(shí)去創(chuàng)建所有的單例bean對(duì)象, 而@Lazy注解可以延遲加載bean對(duì)象,即在使用時(shí)才去初始化.
所以,@Lazy注解, 一是可以減少Spring的IOC容器啟動(dòng)時(shí)的加載時(shí)間, 二是可以解決bean的循環(huán)依賴問題
1 @Lazy的簡(jiǎn)介
@Lazy注解用于標(biāo)識(shí)bean是否需要延遲加載.
@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;
}查看注解源碼可知,只有一個(gè)參數(shù), 默認(rèn)為true, 即添加該注解的bean對(duì)象就會(huì)延遲初始化.
2 @Lazy的使用
以SpringBoot環(huán)境為例
1 準(zhǔn)備一個(gè)Springboot環(huán)境
2 準(zhǔn)備兩個(gè)實(shí)體類對(duì)象
@Data
@NoArgsConstructor
public class User {
private String name;
private String phone;
private Integer age;
private Person person;
public User(String name, String phone, Integer age) {
System.out.println("我User被初始化了.............");
this.name = name;
this.phone = phone;
this.age = age;
}
}@Data
@NoArgsConstructor
public class Person {
private String name;
private String phone;
private Integer age;
private User user;
public Person(String name, String phone, Integer age) {
System.out.println("我Person被初始化了.............");
this.name = name;
this.phone = phone;
this.age = age;
}
}3 添加啟動(dòng)類
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public User createUser() {
return new User("韓宣生", "11111", 24);
}
@Bean
@Lazy
public Person createPerson() {
return new Person("韓立", "11111", 24);
}
}4 測(cè)試查看控制臺(tái)
我User被初始化了.............
5 去掉Person上的 @Lazy注解,重啟項(xiàng)目
我User被初始化了.............
我Person被初始化了.............
3 @Lazy的作用
1 延遲加載bean對(duì)象(如上案列)
2 解決循環(huán)依賴問題
1 添加兩個(gè)配置類
@Component
public class PersonConfig {
private UserConfig userConfig;
public PersonConfig( UserConfig userConfig) {
this.userConfig = userConfig;
System.out.println("我是用戶配置 PersonConfig");
}
}@Component
public class UserConfig {
private PersonConfig personConfig;
public UserConfig(PersonConfig personConfig) {
this.personConfig = personConfig;
System.out.println("我是用戶配置 UserConfig");
}
}2 重啟項(xiàng)目, 項(xiàng)目報(bào)錯(cuò),代碼中存在循環(huán)依賴
Description: The dependencies of some of the beans in the application context form a cycle:
解決辦法,給其中一個(gè)添加@Lazy注解,如
@Component
public class PersonConfig {
private UserConfig userConfig;
public PersonConfig(@Lazy UserConfig userConfig) {
this.userConfig = userConfig;
System.out.println("我是用戶配置 PersonConfig");
}
}3 重啟項(xiàng)目,查看日志
我是用戶配置 PersonConfig
我是用戶配置 UserConfig
4 錯(cuò)誤總結(jié)
1 在項(xiàng)目啟動(dòng)過程中, 遇到異常錯(cuò)誤
'url' attribute is not specified and no embedded datasource could be configu
解決方法: 是項(xiàng)目沒有將application.yml配置文件加載. 點(diǎn)擊maven中clean一下項(xiàng)目, 重啟項(xiàng)目即可.
到此這篇關(guān)于Spring中@Lazy注解的使用的文章就介紹到這了,更多相關(guān)Spring @Lazy注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jpa實(shí)體@ManyToOne @OneToMany無限遞歸方式
這篇文章主要介紹了jpa實(shí)體@ManyToOne @OneToMany無限遞歸方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
在IntelliJ IDEA中使用gulp的方法步驟(圖文)
這篇文章主要介紹了在IntelliJ IDEA中使用gulp的方法步驟(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
Spring Boot學(xué)習(xí)入門之AOP處理請(qǐng)求詳解
AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預(yù)編譯方式和運(yùn)行期動(dòng)態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù),下面這篇文章主要給大家介紹了關(guān)于Spring Boot學(xué)習(xí)入門之AOP處理請(qǐng)求的相關(guān)資料,需要的朋友可以參考下。2017-09-09
SpringBoot中Controller的傳參方式詳細(xì)講解
這篇文章主要介紹了SpringBoot在Controller層接收參數(shù)的常用方法,Controller接收參數(shù)的常用方式總體可以分為三類,第一類是Get請(qǐng)求通過拼接url進(jìn)行傳遞,第二類是Post請(qǐng)求通過請(qǐng)求體進(jìn)行傳遞,第三類是通過請(qǐng)求頭部進(jìn)行參數(shù)傳遞,下面我們來詳細(xì)看看2023-01-01
Spring?MVC中的Controller進(jìn)行單元測(cè)試的實(shí)現(xiàn)
本文主要介紹了如何對(duì)Spring?MVC中的Controller進(jìn)行單元測(cè)試的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
詳解SpringSecurity如何實(shí)現(xiàn)前后端分離
這篇文章主要為大家介紹了詳解SpringSecurity如何實(shí)現(xiàn)前后端分離,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

