Spring Boot基本使用

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容