springboot配置文件自定义字段在dev/prod下的自动读取

读取自定义字段

1.定义字段

application.properties

path.pictrue.brand=/home/abc/Pictures/brand/

application-prod.properties

path.pictrue.brand=/home/xyz/Pictures/brand/

2.读取字段

PropertiesConfig.java

@Component
public class PropertiesConfig {

    @Value("${path.pictrue.brand}")
    public String pathBrand;
}

3.在需要使用的类中:

@Autowired
private PropertiesConfig pConfig;


public void testPicture() {
        
    System.out.println(pConfig.pathBrand);
}

4.生产jar包后:

java -jar xxxxx.jar 使用的是application.properties

java -jar -Dspring.profiles.active=prod xxxxx.jar 则使用的是application-prod.properties

 

posted @ 2020-05-19 20:16  jason47  阅读(1166)  评论(0编辑  收藏  举报