Spring Boot 精要
1, 自動配置
自動配置需要的bean
2, 起步依賴
指定所需要的功能,引入需要的包(包括版本,經(jīng)過測試,放心使用)
3, 命令行界面
Spring Boot CLI 自動檢測使用了哪些類,知道要向Class path添加哪些起步依賴
4, Actuator
提供在運(yùn)行時檢視應(yīng)用程序內(nèi)部情況的能力:
- Spring上下文配置的Bean
- Spring Boot 自動配置做的決策
- 應(yīng)用程序取到的環(huán)境變量,系統(tǒng)屬性,配置屬性,和命令行參數(shù)
- 應(yīng)用程序線程的當(dāng)前狀態(tài)
- 應(yīng)用程序最近處理過的HTTP請求的追蹤情況
- 各種和內(nèi)存用量,垃圾回收,Web請求以及數(shù)據(jù)源用量相關(guān)的指標(biāo)
Spring Boot 條件化配置
加入Spring Boot 時,會加入 spirng-boot-autoconfigure 的jar文件,其中包含很多配置類,利用spring的條件化配置選擇是否自動配置:
編寫自己的條件:
實(shí)現(xiàn)Condition接口,覆蓋matches() 方法
package com.luty.serviceCenter;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class JdbcTemplateCondition implements Condition {
@Override
public boolean matches(ConditionContext Context, AnnotatedTypeMetadata arg1) {
try {
Context.getClassLoader().loadClass("org.springframework.jdbc.core.JdbcTemplate");
return true;
} catch (Exception e) {
return false;
}
}
}
使用條件話配置化注解
| 條件化注解 | 配置生效條件 |
|---|---|
| @ConditionOnBean | 配置了某個特定Bean |