1. 程式人生 > >Spring Boot屬性配置檔案實戰

Spring Boot屬性配置檔案實戰

一 新建pom

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

二 配置屬性檔案

application.properties

com.didispace.blog.name=程式猿DD
com.didispace.blog.title=Spring Boot教程
#在application.properties中的各個引數之間也可以直接引用來使用
com.didispace.blog.desc=${com.didispace.blog.name}正在努力寫《${com.didispace.blog.title}》

# 隨機字串
com.didispace.blog.value=${random.value}
# 隨機int
com.didispace.blog.number=${random.int}
# 隨機long
com.didispace.blog.bignumber=${random.long}
# 10以內的隨機數
com.didispace.blog.test1=${random.int(10)}
# 10-20的隨機數
com.didispace.blog.test2=${random.int[10,20]}

# 多環境配置檔案啟用屬性
spring.profiles.active=dev

application-dev.properties

# 服務埠
server.port=1111

application-prod.properties

# 服務埠
server.port=3333

application-test.properties

# 服務埠
server.port=2222

三 BlogProperties

package com.didispace.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class BlogProperties {
    //通過@Value("${屬性名}")註解來載入對應的配置屬性
    @Value("${com.didispace.blog.name}")
    private String name;
    @Value("${com.didispace.blog.title}")
    private String title;
    @Value("${com.didispace.blog.desc}")
    private String desc;

    @Value("${com.didispace.blog.value}")
    private String value;
    @Value("${com.didispace.blog.number}")
    private Integer number;
    @Value("${com.didispace.blog.bignumber}")
    private Long bignumber;
    @Value("${com.didispace.blog.test1}")
    private Integer test1;
    @Value("${com.didispace.blog.test2}")
    private Integer test2;

    public String getName() {
        return name;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getValue() {
        return value;
    }

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

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public Long getBignumber() {
        return bignumber;
    }

    public void setBignumber(Long bignumber) {
        this.bignumber = bignumber;
    }

    public Integer getTest1() {
        return test1;
    }

    public void setTest1(Integer test1) {
        this.test1 = test1;
    }

    public Integer getTest2() {
        return test2;
    }

    public void setTest2(Integer test2) {
        this.test2 = test2;
    }
}

四 控制器

package com.didispace.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String index() {
        return "Hello World";
    }

}

五 啟動類

package com.didispace;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

}

六 測試類

package com.didispace;

import com.didispace.service.BlogProperties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTests {

    private static final Log log = LogFactory.getLog(ApplicationTests.class);

    @Autowired
    private BlogProperties blogProperties;


    @Test
    public void test1() throws Exception {
        Assert.assertEquals("程式猿DD", blogProperties.getName());
        Assert.assertEquals("Spring Boot教程", blogProperties.getTitle());
        Assert.assertEquals("程式猿DD正在努力寫《Spring Boot教程》", blogProperties.getDesc());

        log.info("隨機數測試輸出:");
        log.info("隨機字串 : " + blogProperties.getValue());
        log.info("隨機int : " + blogProperties.getNumber());
        log.info("隨機long : " + blogProperties.getBignumber());
        log.info("隨機10以下 : " + blogProperties.getTest1());
        log.info("隨機10-20 : " + blogProperties.getTest2());

    }

}

七 測試結果

2018-10-23 18:54:24.493  INFO 11396 --- [           main] com.didispace.ApplicationTests           : 隨機數測試輸出:
2018-10-23 18:54:24.493  INFO 11396 --- [           main] com.didispace.ApplicationTests           : 隨機字串 : e43671b09139453c06c7686b9dbb22bf
2018-10-23 18:54:24.493  INFO 11396 --- [           main] com.didispace.ApplicationTests           : 隨機int : 1573829413
2018-10-23 18:54:24.493  INFO 11396 --- [           main] com.didispace.ApplicationTests           : 隨機long : -2379855551713179344
2018-10-23 18:54:24.493  INFO 11396 --- [           main] com.didispace.ApplicationTests           : 隨機10以下 : 9
2018-10-23 18:54:24.493  INFO 11396 --- [           main] com.didispace.ApplicationTests           : 隨機10-20 : 14

八 說明

1 通過命令列設定屬性值

java -jar xxx.jar --server.port=8888,通過使用--server.port屬性來設定xxx.jar應用的埠為8888。

在命令列執行時,連續的兩個減號--就是對application.properties中的屬性值進行賦值的標識。所以,java -jar xxx.jar --server.port=8888命令,等價於我們在application.properties中新增屬性server.port=8888

通過命令列來修改屬性值固然提供了不錯的便利性,但是通過命令列就能更改應用執行的引數,那豈不是很不安全?是的,所以Spring Boot也貼心的提供了遮蔽命令列訪問屬性的設定,只需要這句設定就能遮蔽:SpringApplication.setAddCommandLineProperties(false)。

2 多環境配置多環境配置

我們在開發Spring Boot應用時,通常同一套程式會被應用和安裝到幾個不同的環境,比如:開發、測試、生產等。其中每個環境的資料庫地址、伺服器埠等等配置都會不同,如果在為不同環境打包時都要頻繁修改配置檔案的話,那必將是個非常繁瑣且容易發生錯誤的事。

對於多環境的配置,各種專案構建工具或是框架的基本思路是一致的,通過配置多份不同環境的配置檔案,再通過打包命令指定需要打包的內容之後進行區分打包,Spring Boot也不例外,或者說更加簡單。

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

  • application-dev.properties:開發環境

  • application-test.properties:測試環境

  • application-prod.properties:生產環境

至於哪個具體的配置檔案會被載入,需要在application.properties檔案中通過spring.profiles.active屬性來設定,其值對應{profile}值。

測試不同配置的載入

  • 執行java -jar xxx.jar,可以觀察到服務埠被設定為1111,也就是預設的開發環境(dev)

  • 執行java -jar xxx.jar --spring.profiles.active=test,可以觀察到服務埠被設定為2222,也就是測試環境的配置(test)

  • 執行java -jar xxx.jar --spring.profiles.active=prod,可以觀察到服務埠被設定為3333,也就是生產環境的配置(prod)

總結多環境的配置思路

  • application.properties中配置通用內容,並設定spring.profiles.active=dev,以開發環境為預設配置。

  • application-{profile}.properties中配置各個環境不同的內容。

  • 通過命令列方式去啟用不同環境的配置。