欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring中@Lazy注解的使用示例教程

 更新時間:2023年06月26日 15:16:19   作者:韓_師兄  
Spring在應用程序上下文啟動時去創(chuàng)建所有的單例bean對象, 而@Lazy注解可以延遲加載bean對象,即在使用時才去初始化,這篇文章主要介紹了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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • MybatisPlus中的insert操作詳解

    MybatisPlus中的insert操作詳解

    這篇文章主要介紹了MybatisPlus中的insert操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java使用NIO包實現(xiàn)Socket通信的實例代碼

    Java使用NIO包實現(xiàn)Socket通信的實例代碼

    本篇文章主要介紹了Java使用NIO包實現(xiàn)Socket通信的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • HttpsURLConnection上傳文件流(實例講解)

    HttpsURLConnection上傳文件流(實例講解)

    下面小編就為大家?guī)硪黄狧ttpsURLConnection上傳文件流(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • 使用Spring Cache時設置緩存鍵的注意事項詳解

    使用Spring Cache時設置緩存鍵的注意事項詳解

    在現(xiàn)代的Web應用中,緩存是提高系統(tǒng)性能和響應速度的重要手段之一,Spring框架提供了強大的緩存支持,通過??@Cacheable??、??@CachePut??、??@CacheEvict??等注解可以方便地實現(xiàn)緩存功能,本文給大家介紹了使用Spring Cache時設置緩存鍵的注意事項
    2025-01-01
  • Java?Cookie與Session實現(xiàn)會話跟蹤詳解

    Java?Cookie與Session實現(xiàn)會話跟蹤詳解

    session的工作原理和cookie非常類似,在cookie中存放一個sessionID,真實的數(shù)據(jù)存放在服務器端,客戶端每次發(fā)送請求的時候帶上sessionID,服務端根據(jù)sessionID進行數(shù)據(jù)的響應
    2022-11-11
  • java實現(xiàn)超市管理系統(tǒng)

    java實現(xiàn)超市管理系統(tǒng)

    這篇文章主要為大家詳細介紹了java實現(xiàn)超市管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Java集合和IO流實現(xiàn)水果攤項目

    Java集合和IO流實現(xiàn)水果攤項目

    最近閑來無事,使用java基礎知識集合和IO流做了一個簡單的小項目,水果攤項目,用到GUI和Mysql數(shù)據(jù)庫搭建,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2021-06-06
  • 聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別

    聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別

    這篇文章主要介紹了聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • SpringMVC 跨重定向請求傳遞數(shù)據(jù)的方法實現(xiàn)

    SpringMVC 跨重定向請求傳遞數(shù)據(jù)的方法實現(xiàn)

    這篇文章主要介紹了SpringMVC 跨重定向請求傳遞數(shù)據(jù)的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • springboot-dubbo cannot be cast to問題及解決

    springboot-dubbo cannot be cast to問題及解決

    這篇文章主要介紹了springboot-dubbo cannot be cast to問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04

最新評論