1. 程式人生 > >Spring Boot 常用配置以及自定義配置

Spring Boot 常用配置以及自定義配置

原文地址:https://renguangli.com/articles/spring-boot-config

Spring Boot 常用配置簡單介紹及使用

多環境配置

Spring Boot Profile

在 Spring Boot 中多環境配置檔名需要滿足 application-{profile}.properties 的格式,其中 {profile} 對應環境標識

  • application-dev.properties 開發環境
  • application-test.properties 測試環境
  • application-prod.properties 生產環境

在 application.properties 檔案中通過 spring.profiles.active 屬性來設定,其值對應 {profile} 值。比如:

spring.profiles.active=dev

Spring Boot 在啟動時會載入 application-dev.properties 配置檔案

Maven Profile

如果我們使用的構建工具是 Maven,也可以通過 Maven 的 Profile 特性來實現多環境配置以及打包。這樣做的好處是我們不需要每次打包的時候改變 spring.profiles.active 的值,打包的時候 mvn clean package -P ${profile}

使用 -P 引數指定

pom.xml 配置如下:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profileActive>dev</profileActive>
        </properties>
        <activation>
            <!-- 開發啟動 Spring Boot 時預設載入 application-dev.properties -->
<activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>test</id> <properties> <profileActive>test</profileActive> </properties> </profile> <profile> <id>prod</id> <properties> <profileActive>prod</profileActive> </properties> </profile> </profiles> <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>application-dev.properties</exclude> <exclude>application-test.properties</exclude> <exclude>application-prod.properties</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application.properties</include> <include>application-${profileActive}.properties</include> </includes> </resource> </resources> </build>

application.properties 配置檔案如下

## profile (dev,test,prod) default dev
[email protected]@

日誌配置

Spring Boot 使用 Commons Logging 進行日誌記錄,但保留底層日誌實現(Java Util Logging, Log4J2 and Logback),預設使用 Logback 作為底層實現。

日誌常用配置


## Logback 日誌級別 ERROR, WARN, INFO, DEBUG or TRACE
logging.level.com.renguangli.config=debug
logging.level.org.springframework.web=info

## 日誌檔名稱 相對或絕對路徑
logging.file=./logs/springboot.log
## 控制檯日誌格式
# logging.pattern.console=
## 日誌檔案格式
# logging.pattern.file=

除了在 application.properties 檔案中配置日誌外還可以使用 xml 配置檔案格式。對於 Logback 日誌框架來說 Spring Boot 預設載入檔案為名為 logback-spring.xml, logback.xml 的配置檔案,log4j2 預設載入 log4j2-spring.xml or log4j2.xml 的配置檔案。當讓也可以使用如下配置指定:

logging.config=classpath:logback.xml

下面是一個完整的 Logback 日誌配置檔案


下面是一個完整的 log4j2 日誌配置檔案


自定義配置

@Value

config.name=hello
config.value=world

對於像上面的自定義配置我們可以這樣使用

@Value("${config.name}")
private String name;

@Value("${config.value}")
private String value;

一兩個配置的話還好,一旦配置多起來就比較難看,對於這種情況我們可以使用 Spring Boot 提供的 @ConfigurationProperties 註解來簡化配置

配置類

@Component
@ConfigurationProperties(prefix = "config")
public class ConfigProperties {

    private String name;

    private String value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

如何使用呢?

@Autowired
private ConfigProperties configProperties;

自定義配置自動提示

在 pom.xml 檔案中新增依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

第二步我們需要配置META-INF/spring-configuration-metadata.json檔案來描述。為了方便我們可以通過 idea 自動生成。

在 idea 設定中搜索 Annotation Processors,接下來勾住 Enable annonation processing 就完成了。

我們可以在編譯後的檔案中看到自動生成的spring-configuration-metadata.json。

imgaes

imgaes

參考資料

boot-features-logging

Springboot之自定義配置

Spring Boot Profiles實現多環境下配置切換

示例程式碼在 Github 上地址:spring-boot-config