当前位置:   article > 正文

Spring Boot 配置文件之 多环境变量-三_comma-separated list of active profiles. can be ov

comma-separated list of active profiles. can be overridden by a command line

多环境

通过在application.properties中配置下面的属性来实现

# PROFILES
spring.profiles.active= # Comma-separated list of active profiles. Can be overridden by a command line switch.
  • 1
  • 2

在这里插入图片描述

dev
## 家乡属性 Dev
home.province=广东-dev
home.city=深圳-dev
home.desc=我生活在 ${home.province} ${home.city}.

  • 1
  • 2
  • 3
  • 4
  • 5
test
## 家乡属性 Dev
home.province=广东-tet
home.city=深圳-test
home.desc=我生活在 ${home.province} ${home.city}.

  • 1
  • 2
  • 3
  • 4
  • 5
prep
## 家乡属性 Dev
home.province=广东-prep
home.city=深圳-prep
home.desc=我生活在 ${home.province} ${home.city}.

  • 1
  • 2
  • 3
  • 4
  • 5
prod
## 家乡属性 Dev
home.province=广东-prod
home.city=深圳-prod
home.desc=我生活在 ${home.province} ${home.city}.

  • 1
  • 2
  • 3
  • 4
  • 5
acpplication.properties中配置
#profiles
spring.profiles.active=dev
  • 1
  • 2
属性注入
package com.ghgcn.hello.config;

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

@Component
@ConfigurationProperties(prefix = "home")
public class HomeProperties {


    private String province;

    private String city;

    private String desc;


    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getDesc() {
        return desc;
    }

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

    @Override
    public String toString() {
        return "HomeProperties{" +
                "province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}

  • 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
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
controller
@RestController
public class HelloController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private HomeProperties homeProperties;


    @GetMapping("/hello/home")
    public HomeProperties getHome(){
        logger.info(homeProperties.toString());
        return homeProperties;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
访问

在这里插入图片描述
切换为test
在这里插入图片描述

只需要在application.properties中替换变量即可

#profiles
spring.profiles.active=${变量}
  • 1
  • 2
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号