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

