Spring Boot中的自定義自動配置

2021-10-05 08:34:21 字數 854 閱讀 4820

本部落格的目的是了解springboot提供的自動配置。 我將建立乙個用於演示目的的日誌記錄庫,其中將包括auto-configuration類,以為該庫建立所有必需的bean。

我正在使用gradle多模組專案。 模組日誌庫將是乙個共享庫,將在服務模組。

now it's time to create required config classes. i've created a config class calledloggingautoconfigurationand marked as@configurationand created one conditional bean.

@configuration

public

class

loggingautoconfiguration

}

到目前為止,該類僅是普通的spring配置,要使其成為自動配置類,我們需要執行2個步驟:

在以下位置建立檔案日誌庫有名字彈簧工廠下資源\ meta-inf \目錄。

現在,將您的配置類新增到彈簧工廠。

org.springframework.boot.autoconfigure.enableautoconfiguration

=tutorials.logging.loggingautoconfiguration

而已!!!

現在我們可以將模組注入服務而且我們不需要指定任何要掃瞄的軟體包日誌庫我們的自動配置類將解決這一問題。

you can find the running code here

from:

springboot中自定義退出碼

public static void main string args override public int getexitcode 這裡只需要實現exitcodegenerator就可以了,重寫getexitcode 方法,返回自定義碼。而且我們可以監聽退出的事件 bean sampleeven...

自定義Spring Boot裝配

spring boot自動配置會嘗試根據新增的jar依賴項自動配置spring應用程式。使用 componentscan 查詢您的bean 和使用 autowired 進行建構函式注入 自動配置類使用 conditionalonclass和 conditionalo singbean注釋,condi...

springboot自定義事務

1.在springboot專案中service的實現類可以通過註解 transactional新增事務 1.1 如果在service層用了try catch,在catch裡面再丟擲乙個 runtimeexception異常,這樣出了異常才會回滾 1.2你還可以直接在catch後面寫一句回滾 tran...