1. 程式人生 > >轉載:SpringBoot非官方教程 | 第二篇:Spring Boot配置檔案詳解

轉載:SpringBoot非官方教程 | 第二篇:Spring Boot配置檔案詳解

springboot採納了建立生產就緒Spring應用程式的觀點。 Spring Boot優先於配置的慣例,旨在讓您儘快啟動和執行。在一般情況下,我們不需要做太多的配置就能夠讓spring boot正常執行。在一些特殊的情況下,我們需要做修改一些配置,或者需要有自己的配置屬性。

一、自定義屬性

當我們建立一個springboot專案的時候,系統預設會為我們在src/main/java/resources目錄下建立一個application.properties。個人習慣,我會將application.properties改為application.yml檔案,兩種檔案格式都支援。

在application.yml自定義一組屬性:

my:
 name: forezp
 age: 12
  • 1
  • 2
  • 3
  • 4

如果你需要讀取配置檔案的值只需要加@Value(“${屬性名}”):

@RestController
public class MiyaController {

    @Value("${my.name}")
    private String name;
    @Value("${my.age}")
    private int age;

    @RequestMapping(value = "/miya")
    public String miya(){
        return name+":"+age;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

啟動工程,訪問:localhost:8080/miya,瀏覽器顯示:

forezp:12

二、將配置檔案的屬性賦給實體類

當我們有很多配置屬性的時候,這時我們會把這些屬性作為欄位來建立一個javabean,並將屬性值賦予給他們,比如:

my:
 name: forezp
 age: 12
 number:  ${random.int}
 uuid : ${random.uuid}
 max: ${random.int(10)}
 value: ${random.value}
 greeting: hi,i'm  ${my.name}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

其中配置檔案中用到了${random} ,它可以用來生成各種不同型別的隨機值。

怎麼講這些屬性賦於給一個javabean 呢,首先建立一個javabean :

@ConfigurationProperties(prefix = "my")
@Component
public class ConfigBean {

    private String name;
    private int age;
    private int number;
    private String uuid;
    private int max;
    private String value;
    private String greeting;

    省略了getter setter....
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

需要加個註解@ConfigurationProperties,並加上它的prrfix。另外@Component可加可不加。另外spring-boot-configuration-processor依賴可加可不加,具體原因不詳。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

另外需要在應用類或者application類,加EnableConfigurationProperties註解。


@RestController
@EnableConfigurationProperties({ConfigBean.class})
public class LucyController {
    @Autowired
    ConfigBean configBean;

    @RequestMapping(value = "/lucy")
    public String miya(){
        return configBean.getGreeting()+" >>>>"+configBean.getName()+" >>>>"+ configBean.getUuid()+" >>>>"+configBean.getMax();
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

啟動工程,訪問localhost:8080/lucy,我們會發現配置檔案資訊讀到了。

三、自定義配置檔案

上面介紹的是我們都把配置檔案寫到application.yml中。有時我們不願意把配置都寫到application配置檔案中,這時需要我們自定義配置檔案,比如test.properties:

com.forezp.name=forezp
com.forezp.age=12
  • 1
  • 2
  • 3

怎麼將這個配置檔案資訊賦予給一個javabean呢?

@Configuration
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "com.forezp")
public class User {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

在最新版本的springboot,需要加這三個註解。@Configuration @PropertySource(value = “classpath:test.properties”) @ConfigurationProperties(prefix = “com.forezp”);在1.4版本需要 PropertySource加上location。

@RestController
@EnableConfigurationProperties({ConfigBean.class,User.class})
public class LucyController {
    @Autowired
    ConfigBean configBean;

    @RequestMapping(value = "/lucy")
    public String miya(){
        return configBean.getGreeting()+" >>>>"+configBean.getName()+" >>>>"+ configBean.getUuid()+" >>>>"+configBean.getMax();
    }

    @Autowired
    User user;
    @RequestMapping(value = "/user")
    public String user(){
        return user.getName()+user.getAge();
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

啟動工程,開啟localhost:8080/user;瀏覽器會顯示:

forezp12

四、多個環境配置檔案

在現實的開發環境中,我們需要不同的配置環境;格式為application-{profile}.properties,其中{profile}對應你的環境標識,比如:

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

怎麼使用?只需要我們在application.yml中加:

 spring:
  profiles:
    active: dev
  • 1
  • 2
  • 3
  • 4

其中application-dev.yml:

 server:
  port: 8082
  • 1
  • 2
  • 3

啟動工程,發現程式的埠不再是8080,而是8082。

五、參考文獻

優秀文章推薦: