• 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);
        }
    }
  • 相关阅读:
    D. Time to Run【构造】
    P3388 割顶 【求割点个数】
    处女座的测验 素数,构造
    处女座与复读机 DP
    求一个分数小数点后指定位数的数字
    安卓开发创建活动,布局,添加按钮,she使用Toast,设菜单,使菜单相关联等操作
    三进制 处女座的砝码 高精度
    上海高校程序设计联赛 D-CSL的字符串 栈模拟
    区间DP经典 石子合并
    区间DP 洛谷P2858牛奶零食
  • 原文地址:https://www.cnblogs.com/Ly66310/p/15986767.html
Copyright © 2020-2023  润新知