• 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

    逃避不一定躲得过,面对不一定最难过
  • 相关阅读:
    CSS3中的3D效果
    JavaScript判断数据类型方法?
    JS函数中的arguments是什么?
    Vue组件之间通信的几种方式
    Vue插槽详解
    CSS文本溢出效果&滚动条样式设置
    Fibonacci数列计算的三种方法
    堆内存和栈内存详解[转]
    带头结点的单链表反转
    汉诺塔问题
  • 原文地址:https://www.cnblogs.com/yangzhenlong/p/6383200.html
Copyright © 2020-2023  润新知