• spring boot配置多环境资源文件


    项目结构

    文件格式

    #{profile}自定义的不同环境标识
    application-{profile}.properties    

    测试

    文件内容

    #application-dev.properties
    com.sxd.ip = 127.0.0.1
    com.sxd.value = 开发环境
    
    #application-test.properties
    com.sxd.ip = 192.168.15.14
    com.sxd.value = 测试环境
    
    #application-pro.properties
    com.sxd.ip = 102.118.174.14
    com.sxd.value = 生产环境
    
    #application.properties 
    #选择开发环境资源文件,可多选
    spring.profiles.active=dev    
    #spring.profiles.include=dev,test,pro

    绑定使用

    package com.sxd.beans;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties(prefix = "com.sxd")
    public class ConfigBean {
    
        private String ip;
        private String value;
    
        public String getIp() {
            return ip;
        }
    
        public void setIp(String ip) {
            this.ip = ip;
        }
    
        public String getValue() {
            return value;
        }
    
        public void setValue(String value) {
            this.value = value;
        }
    }

    运行校验

    package com.sxd.firstdemo;
    
    import com.sxd.beans.ConfigBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @SpringBootApplication
    @EnableConfigurationProperties({ConfigBean.class})
    public class FirstdemoApplication {
    
        @Autowired
        ConfigBean configBean;
    
        @RequestMapping("/")
        public String index(){
    
            return "IP:"+configBean.getIp()+"\n环境:"+configBean.getValue();
        }
        public static void main(String[] args) {
            SpringApplication.run(FirstdemoApplication.class, args);
        }
    }
  • 相关阅读:
    9-基础练习 十进制转十六进制
    8-十六进制转十进制
    16-acrobat por 简单使用指南
    LightOJ 1245
    LightOJ 1234
    Codeforces 479【E】div3
    Codeforces 479【F】div3
    Codeforces 479【D】div3
    Codeforces 479【C】div3
    Codeforces 479【B】div3
  • 原文地址:https://www.cnblogs.com/Ly66310/p/15986767.html
Copyright © 2020-2023  润新知