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)文章
Java使用NIO包實(shí)現(xiàn)Socket通信的實(shí)例代碼
本篇文章主要介紹了Java使用NIO包實(shí)現(xiàn)Socket通信的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
HttpsURLConnection上傳文件流(實(shí)例講解)
下面小編就為大家?guī)硪黄狧ttpsURLConnection上傳文件流(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
使用Spring Cache時(shí)設(shè)置緩存鍵的注意事項(xiàng)詳解
在現(xiàn)代的Web應(yīng)用中,緩存是提高系統(tǒng)性能和響應(yīng)速度的重要手段之一,Spring框架提供了強(qiáng)大的緩存支持,通過??@Cacheable??、??@CachePut??、??@CacheEvict??等注解可以方便地實(shí)現(xiàn)緩存功能,本文給大家介紹了使用Spring Cache時(shí)設(shè)置緩存鍵的注意事項(xiàng)2025-01-01
Java?Cookie與Session實(shí)現(xiàn)會(huì)話跟蹤詳解
session的工作原理和cookie非常類似,在cookie中存放一個(gè)sessionID,真實(shí)的數(shù)據(jù)存放在服務(wù)器端,客戶端每次發(fā)送請(qǐng)求的時(shí)候帶上sessionID,服務(wù)端根據(jù)sessionID進(jìn)行數(shù)據(jù)的響應(yīng)2022-11-11
Java集合和IO流實(shí)現(xiàn)水果攤項(xiàng)目
最近閑來無事,使用java基礎(chǔ)知識(shí)集合和IO流做了一個(gè)簡(jiǎn)單的小項(xiàng)目,水果攤項(xiàng)目,用到GUI和Mysql數(shù)據(jù)庫(kù)搭建,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-06-06
聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別
這篇文章主要介紹了聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
SpringMVC 跨重定向請(qǐng)求傳遞數(shù)據(jù)的方法實(shí)現(xiàn)
這篇文章主要介紹了SpringMVC 跨重定向請(qǐng)求傳遞數(shù)據(jù)的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
springboot-dubbo cannot be cast to問題及解決
這篇文章主要介紹了springboot-dubbo cannot be cast to問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

