Spring Boot入門(二):使用Profile實現多環境配置管理&獲取配置檔案值的兩種方式

周偉偉的技術部落格發表於2019-04-04

在上一篇部落格Spring Boot入門(一):使用IDEA建立Spring Boot專案並使用yaml配置檔案中,我們新建了一個最原始的Spring Boot專案,並使用了更為流行的yaml配置檔案。

但是一般情況下,我們開發的系統應用都會有多套環境, 如dev環境,qa環境,prod環境,

那麼如何實現多套環境下的配置管理呢?

其實在Spring Boot下,我們可以使用Profile來實現,以下來講解具體的實現方式。

1.使用Profile實現多環境配置管理

首先我們按照上篇部落格中提到的方法新建2個配置檔案:application-dev.yml,application-prod.yml。

如果有的同學比較喜歡用properties檔案,可以用下圖中的方式新建:

img

img

我們知道,預設情況下,啟動的埠號為8080,如果我們希望在dev環境使用埠號8082,在prod環境使用埠號8083,那麼可以修改配置檔案如下:

application-dev.yml新增如下配置:

server:
  port: 8082
複製程式碼

application-prod.yml新增如下配置:

server:
  port: 8083
複製程式碼

此時,啟動下Spring Boot專案

img

我們會發現,仍然使用的是預設的埠號8080,那麼如何指定使用dev或者prod環境的埠呢?

我們需要在application.yml新增如下配置:

spring:
  profiles:
    active: dev
複製程式碼

此時,再次啟動Spring Boot專案,會發現使用的是埠號8082,也就是application-dev.yml檔案中配置的。

img

如果希望使用prod環境的,可以修改配置為:

spring:
  profiles:
    active: prod
複製程式碼

執行結果為:

img

2.獲取配置檔案值的兩種方式

既然用到了配置檔案,而且在平時的開發過程中,經常會將一些可能會修改的值放到配置檔案中,那麼在Spring Boot中,如何獲取配置的檔案的值呢?

我們現在就講解下使用@Value註解或者@ConfigurationProperties註解來獲取配置檔案值的方法。

2.1方式1:使用@Value註解獲取配置檔案值

首先在application.yml中新增如下配置:

book:
  author: wangyunfei
  name: spring boot
複製程式碼

然後修改Spring Boot專案啟動類的程式碼如下:

package com.zwwhnly.springbootdemo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class SpringbootdemoApplication {

    @Value("${book.author}")
    private String bookAuthor;
    @Value("${book.name}")
    private String bookName;

    @RequestMapping("/")
    String index() {
        return "book name is:" + bookName + " and book author is:" + bookAuthor;
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }
}
複製程式碼

啟動專案,在瀏覽器輸入http://localhost:8080/,會看到如下資訊:

img

2.2使用@ConfigurationProperties註解獲取配置檔案值

在方式1中,我們使用@Value註解來獲取配置檔案值,但如果多個地方都需要獲取的話,就需要在多個地方寫註解,造成混亂,不好管理,

其實,Spring Boot還提供了@ConfigurationProperties註解來獲取配置檔案值,該種方式可把配置檔案值和一個Bean自動關聯起來,使用起來更加方便,個人建議用這種方式

在application.yml中新增如下配置:

author:
  name: wangyunfei
  age: 32
複製程式碼

新建類AuthorSettings,新增@Component和@ConfigurationProperties註解

package com.zwwhnly.springbootdemo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "author")
public class AuthorSettings {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
複製程式碼

然後修改啟動類程式碼如下:

package com.zwwhnly.springbootdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class SpringbootdemoApplication {

    @Value("${book.author}")
    private String bookAuthor;
    @Value("${book.name}")
    private String bookName;

    @Autowired
    private AuthorSettings authorSettings;

    @RequestMapping
    public String hello() {
        return "Hello Spring Boot!";
    }

    @RequestMapping("/")
    public String index() {
        return "book name is:" + bookName + " and book author is:" + bookAuthor;
    }

    @RequestMapping("/indexV2")
    public String indexV2() {
        return "author name is:" + authorSettings.getName() + " and author age is:" + authorSettings.getAge();
    }


    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }
}
複製程式碼

啟動專案,在瀏覽器輸入http://localhost:8080/indexV2,會看到如下資訊:

Spring Boot入門(二):使用Profile實現多環境配置管理&獲取配置檔案值的兩種方式

3.原始碼地址

github.com/zwwhnly/spr…,歡迎大家下載,有問題可以多多交流。

4.參考連結

SpringBoot - 多Profile使用與切換

IDEA如何建立.properties檔案

相關文章