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

    逃避不一定躲得过,面对不一定最难过
  • 相关阅读:
    18-10-11 关于触发器的学习
    18-10-29 关于设计器机器人等安装遇到的问题的解决方法
    18-10-25 全局函数测试总结 创建时间的目录 网页获取数据 写入数据
    18-09-08 关于Linux 的安装遇到的一些小坑
    18-08-27 机器人自动化之页面表格数据的定位拾取
    day 96 关于分页的使用
    day73 母版 中间件
    通过 U 盘启动重装 macOS 系统
    MAMP 环境下为 php 添加 pcntl 扩展
    使用 Composer 安装 Laravel 框架
  • 原文地址:https://www.cnblogs.com/yangzhenlong/p/6383200.html
Copyright © 2011-2022 走看看