• springboot 02-PropertiesFile 自定义配置属性,多环境配置


    application.properties:

    # 自定义配置
    test.hello.world = HelloWorld
    test.person.name = 哈哈
    test.person.sex = 男
    
    # 多环境配置文件激活属性
    spring.profiles.active=dev

    application-dev.properties:

    # 服务端口
    server.port=1111

    application-test.properties:

    # 服务端口
    server.port=2222

    application-prod.properties:

    # 服务端口
    server.port=3333

    TestProperties:

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    /**
     * properties类描述:
     *
     * @author yangzhenlong
     * @since 2017/2/9
     */
    @Component
    public class TestProperties {
    
        @Value("${test.hello.world}")
        private String helloWorld;
        @Value("${test.person.name}")
        private String personName;
        @Value("${test.person.sex}")
        private String personSex;
    
        public String getHelloWorld() {
            return helloWorld;
        }
    
        public void setHelloWorld(String helloWorld) {
            this.helloWorld = helloWorld;
        }
    
        public String getPersonName() {
            return personName;
        }
    
        public void setPersonName(String personName) {
            this.personName = personName;
        }
    
        public String getPersonSex() {
            return personSex;
        }
    
        public void setPersonSex(String personSex) {
            this.personSex = personSex;
        }
    }

    PropertiesController:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * PropertiesController类描述:
     *
     * @author yangzhenlong
     * @since 2017/2/9
     */
    @RestController
    public class PropertiesController {
        @Autowired
        TestProperties testProperties;
    
        @RequestMapping("/properties")
        public String[] getProperties(){
            String[] result = {"hello:" + testProperties.getHelloWorld(),
                    "name:" + testProperties.getPersonName(),
                    "sex:" + testProperties.getPersonSex()};
    
            return result;
        }
    }

    启动app类后,浏览器访问:http://localhost:1111/properties

    逃避不一定躲得过,面对不一定最难过
  • 相关阅读:
    linux命令-定时任务at
    linux网络监控_网速测试
    Linux磁盘分区扩容
    Ubuntu配置SSH服务
    Ubuntu用户管理
    Ubuntu安装lrzsz
    Ubuntu系统配置apt-get软件更新源
    Ubuntu网络配置IP和DNS等,适用于14.04,16.04,17.10和18.04
    Ubuntu系统安装,适用于14.04,16.04和17.10
    使用nginx反向代理处理前后端跨域访问
  • 原文地址:https://www.cnblogs.com/yangzhenlong/p/6383200.html
Copyright © 2020-2023  润新知