SpringBoot properties -- Profiles實現多環境下配置切換

本文目類:

  1. 根據環境更改配置,不同環境配置不同
    1.1 YAML 更改配置
    1.2 application-${profile}.properties 更改配置
  2. @Profile 使用
  3. 工具類

根據環境更改配置

官方文檔

方式一 YAML

新建一個web項目,新建配置文件application.yml:

server:
  port: 9090

spring:
  profiles:
    active: pro
---
#development environment
spring:
  profiles: dev

test_name: dev_testName
---
#production environment
spring:
  profiles: pro

test_name: pro_testName

application.yml文件分爲三部分,使用 --- 來作爲分隔符,第一部分通用配置部分,表示兩個環境都通用的屬性, 後面三段分別爲:開發,生產
profiles.active後面的文字決定 開發模式,還是生成模式

創建TestController:

@RestController
public class HelloController {

    @Value("${test_name}")
    private String testName;

    @GetMapping("/hello")
    public String hello() {
        return testName;
    }
}

啓動項目,訪問http://127.0.0.1:9090/hello

image.png

 

修改application.yml 文件

server:
  port: 9090

spring:
  profiles:
    active: dev
---
#development environment
spring:
  profiles: dev

test_name: dev_testName
---
#production environment
spring:
  profiles: pro

test_name: pro_testName

修改 active: dev
啓動項目,訪問http://127.0.0.1:9090/hello

image.png

 

結論:配置profile.active不同,獲取的test_name的值不同。

方式二:application-${profile}.properties 更改配置

you can use application-${profile}.properties to specify profile-specific values.

resource 目錄下創建文件application-dev.yml,application-pro.yml文件

 

image.png

 

內容分別爲:

test_name: dev_testName_file
test_name: pro_testName_file

application.yml 內容爲:

server:
  port: 9090

spring:
  profiles:
    active: dev

啓動項目測試,和上面結果一樣,不同配置,讀取的配置不同。

@Profile 使用

官方地址

  1. @Profile註解使用範圍:
    @Configration 和 @Component 註解的類及其方法,其中包括繼承了@Component的註解:@Service、@Controller、@Repository等…

  2. @Profile可接受一個或者多個參數

@Service
@Profile({"pro","dev"})

Demo

創建接口類

public interface HelloService {
    String getTestName();
}

創建兩個實現類:

@Service
@Profile("dev")
public class DevHelloServiceImpl implements HelloService {

    @Value("${test_name}")
    private String testName;

    @Override
    public String getTestName() {
        return "開發環境   "+testName;
    }
}
@Service
@Profile("pro")
public class ProHelloServiceImpl implements HelloService {
    @Value("${test_name}")
    private String testName;

    @Override
    public String getTestName() {
        return "生產環境   " + testName;
    }
}

@Profile("pro") 註解代表不同的環境配置。

編寫接口測試:

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/")
    public String getTestName() {
        return helloService.getTestName();
    }
}

當前application.yml文件中 profiles.active: dev
啓動項目,訪問:http://127.0.0.1:9090/
返回如下:

image.png

 

工具類

@Component
public class ProfileUtil implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    /**
     * 獲取當前環境的active profile.
     *
     * @return
     */
    public static String getActiveProfile() {
        String[] activeProfiles = context.getEnvironment().getActiveProfiles();
        if (!ArrayUtils.isEmpty(activeProfiles)) {
            return activeProfiles[0];
        }
        return "";
    }
}

源碼地址:https://github.com/lbshold/springboot/tree/master/Spring-Boot-ProfileDemo

參考文章:
https://blog.csdn.net/zknxx/article/details/77906096
https://blog.csdn.net/fmuma/article/details/82787500

 

 




原文地址:https://www.jianshu.com/p/4d346d3ce45c
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章