zoukankan      html  css  js  c++  java
  • 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

    逃避不一定躲得过,面对不一定最难过
  • 相关阅读:
    laravel 使用DB 鏈接leftJoin查詢
    checkbox复选框,如何让其勾选时触发一个事件,取消勾选时不触发
    js获取上传图片大小,判断上传图片类型,获取图片真实宽度和高度
    如何查看crontab的日志记录
    linux应用之gcc环境的安装
    laravel 获取上一条insert语句产生的id
    laravel多条件查询(and,or嵌套查询)
    laravel ORM 模型关联 with () 用法
    js实现表单提交 onsubmit
    如何利用jquery来给input添加或删除disabled属性
  • 原文地址:https://www.cnblogs.com/yangzhenlong/p/6383200.html
Copyright © 2011-2022 走看看