Spring中@Lazy注解的使用示例教程
Spring在應用程序上下文啟動時去創(chuàng)建所有的單例bean對象, 而@Lazy注解可以延遲加載bean對象,即在使用時才去初始化.
所以,@Lazy注解, 一是可以減少Spring的IOC容器啟動時的加載時間, 二是可以解決bean的循環(huán)依賴問題
1 @Lazy的簡介
@Lazy注解用于標識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ù), 默認為true, 即添加該注解的bean對象就會延遲初始化.
2 @Lazy的使用
以SpringBoot環(huán)境為例
1 準備一個Springboot環(huán)境
2 準備兩個實體類對象
@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 錯誤總結
1 在項目啟動過程中, 遇到異常錯誤
'url' attribute is not specified and no embedded datasource could be configu
解決方法: 是項目沒有將application.yml配置文件加載. 點擊maven中clean一下項目, 重啟項目即可.
到此這篇關于Spring中@Lazy注解的使用的文章就介紹到這了,更多相關Spring @Lazy注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java使用NIO包實現(xiàn)Socket通信的實例代碼
本篇文章主要介紹了Java使用NIO包實現(xiàn)Socket通信的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02Java?Cookie與Session實現(xiàn)會話跟蹤詳解
session的工作原理和cookie非常類似,在cookie中存放一個sessionID,真實的數(shù)據(jù)存放在服務器端,客戶端每次發(fā)送請求的時候帶上sessionID,服務端根據(jù)sessionID進行數(shù)據(jù)的響應2022-11-11聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別
這篇文章主要介紹了聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02SpringMVC 跨重定向請求傳遞數(shù)據(jù)的方法實現(xiàn)
這篇文章主要介紹了SpringMVC 跨重定向請求傳遞數(shù)據(jù)的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06springboot-dubbo cannot be cast to問題及解決
這篇文章主要介紹了springboot-dubbo cannot be cast to問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04