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

淺談SpringBoot中的Bean初始化方法?@PostConstruct

 更新時間:2021年11月29日 11:12:56   作者:Markey92  
這篇文章主要介紹了SpringBoot中的Bean初始化方法?@PostConstruct,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

注解說明

  • 使用注解: @PostConstruct
  • 效果:在Bean初始化之后(構造方法和@Autowired之后)執(zhí)行指定操作。經(jīng)常用在將構造方法中的動作延遲。
  • 備注:Bean初始化時候的執(zhí)行順序: 構造方法 -> @Autowired -> @PostConstruct

代碼示例

注解示例

@Component
public class PostConstructTest1 {
    @Autowired
    PostConstructTest2 postConstructTest2;
    public PostConstructTest1() {
//        postConstructTest2.hello();
    }
    @PostConstruct
    public void init() {
        // some init function
    }
}

在Bean的初始化操作中,有時候會遇到調用其他Bean的時候報空指針錯誤。這時候就可以將調用另一個Bean的方法這個操作放到@PostConstruct注解的方法中,將其延遲執(zhí)行。

錯誤示例

    @Component
    public class PostConstructTest1 {
        @Autowired
        PostConstructTest2 postConstructTest2;
        public PostConstructTest1() {
            postConstructTest2.hello();
        }
    }
    @Component
    public class PostConstructTest2 {
        public void hello() {
            System.out.println("hello, i am PostConstructTest2");
        }
    }

在這里插入圖片描述

正確示例

    @Component
    public class PostConstructTest1 {
        @Autowired
        PostConstructTest2 postConstructTest2;
        public PostConstructTest1() {
            postConstructTest2.hello();
        }
    }
@Component
public class PostConstructTest1 {
    @Autowired
    PostConstructTest2 postConstructTest2;
    public PostConstructTest1() {
//        postConstructTest2.hello();
    }
    @PostConstruct
    public void init() {
        postConstructTest2.hello();
    }
}

在這里插入圖片描述

SpringBoot @PostConstruct雖好,也要慎用

做過SpringBoot開發(fā)的話,肯定對@PostConstruct比較熟悉。在一個Bean組件中,標記了@PostConstruct的方法會在Bean構造完成后自動執(zhí)行方法的邏輯。

1 問題的產(chǎn)生

先說下SpringBoot中Bean的加載過程,簡單點說就是SpringBoot會把標記了Bean相關注解(例如@Component、@Service、@Repository等)的類或接口自動初始化全局的單一實例,如果標記了初始化順序會按照用戶標記的順序,否則按照默認順序初始化。在初始化的過程中,執(zhí)行完一個Bean的構造方法后會執(zhí)行該Bean的@PostConstruct方法(如果有),然后初始化下一個Bean。

那么: 如果@PostConstruct方法內的邏輯處理時間較長,就會增加SpringBoot應用初始化Bean的時間,進而增加應用啟動的時間。因為只有在Bean初始化完成后,SpringBoot應用才會打開端口提供服務,所以在此之前,應用不可訪問。

2 案例模擬

為了模擬上面說的情況,在SpringBoot項目中建兩個組件類ComponentOne和ComponentTwo。耗時的初始化邏輯放在ComponentOne中,并設置ComponentOne的初始化順序在ComponentTwo之前。完整代碼如下:

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ComponentOne {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    public ComponentOne() {
        this.logger.info("ComponentOne 初始化完成");
    }
    @PostConstruct
    public void init() {
        this.logger.info("ComponentOne 模擬耗時邏輯開始");
        try {
        	//這里休眠5秒模擬耗時邏輯
            Thread.sleep(1000 * 5);
        } catch (InterruptedException e) {
            logger.info("模擬邏輯耗時失敗", e);
        }
        this.logger.info("ComponentOne 模擬耗時邏輯完成");
    }
}
@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
public class ComponentTwo {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    public ComponentTwo() {
        this.logger.info("ComponentTwo 初始化完成");
    }
    @PostConstruct
    public void init() {
        this.logger.info("ComponentTwo 初始化完成后處理");
    }
}

啟動應用,初始化部分日志如下:

在這里插入圖片描述

3 總結

所以,如果應用有一些初始化操作,有以下幾點建議:

  • 輕量的邏輯可放在Bean的@PostConstruct方法中
  • 耗時長的邏輯如果放在@PostConstruct方法中,可使用獨立線程執(zhí)行
  • 初始化操作放在CommandLineRunner或ApplicationRunner的實現(xiàn)組件中

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Java 數(shù)據(jù)結構鏈表操作實現(xiàn)代碼

    Java 數(shù)據(jù)結構鏈表操作實現(xiàn)代碼

    這篇文章主要介紹了Java 數(shù)據(jù)結構鏈表操作的相關資料,并附實例代碼,需要的朋友可以參考下
    2016-10-10
  • java中靜態(tài)導入機制用法實例詳解

    java中靜態(tài)導入機制用法實例詳解

    這篇文章主要介紹了java中靜態(tài)導入機制用法實例詳解的相關資料,需要的朋友可以參考下
    2017-07-07
  • Java由淺入深細數(shù)數(shù)組的操作上

    Java由淺入深細數(shù)數(shù)組的操作上

    數(shù)組對于每一門編程語言來說都是重要的數(shù)據(jù)結構之一,當然不同語言對數(shù)組的實現(xiàn)及處理也不盡相同。Java?語言中提供的數(shù)組是用來存儲固定大小的同類型元素
    2022-04-04
  • StringUtils工具包中字符串非空判斷isNotEmpty和isNotBlank的區(qū)別

    StringUtils工具包中字符串非空判斷isNotEmpty和isNotBlank的區(qū)別

    今天小編就為大家分享一篇關于StringUtils工具包中字符串非空判斷isNotEmpty和isNotBlank的區(qū)別,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Java文件大小轉換的兩種方式小結

    Java文件大小轉換的兩種方式小結

    在程序開發(fā)的過程中,文件的大小在視圖呈現(xiàn)和數(shù)據(jù)庫存儲的過程不一致怎么轉換呢,本文主要介紹了Java文件大小轉換的兩種方式小結,具有一定的參考價值,感興趣的可以了解一下
    2024-07-07
  • SpringBoot整合FastJson過程解析

    SpringBoot整合FastJson過程解析

    這篇文章主要介紹了SpringBoot整合FastJson過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10
  • Spring MVC 與 CORS跨域的詳細介紹

    Spring MVC 與 CORS跨域的詳細介紹

    本文介紹了 CORS 的知識以及如何在 Spring MVC 中配置 CORS,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Java mybatis-plus詳解

    Java mybatis-plus詳解

    MyBatis-Plus是一個MyBatis的增強工具,在MyBatis的基礎上只做增強不做修改,為簡化開發(fā)、提高效率而生,本文給大家詳細講解一下MyBatis-Plus,需要的朋友參考下吧
    2021-09-09
  • 使用Mybatis遇到的there is no getter異常

    使用Mybatis遇到的there is no getter異常

    這篇文章主要介紹了使用Mybatis遇到的there is no getter異常,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • 圖解JVM垃圾內存回收算法

    圖解JVM垃圾內存回收算法

    這篇文章主要介紹了圖解JVM垃圾內存回收算法,由于年輕代堆空間的垃圾回收會很頻繁,因此其垃圾回收算法會更加重視回收效率,下面小編就和大家來一起學習一下吧
    2019-06-06

最新評論