国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > Spring Boot 配置文件介绍

Spring Boot 配置文件介绍

来源:程序员人生   发布时间:2017-03-01 08:50:56 阅读次数:2842次
属性文件使用Application/YAML

Application/YAML 属性文件,按优先级按高到低排序,位置高的将覆盖位置低的
    1. 当前目录下的1个/config子目录
    2. 当前目录
    3. 1个classpath下的/config包
    4. classpath根路径(root)

application.properties
##自定义属性
#32位随机字符串
jerome.random.value=${random.value}
#int类型的随机数字
#jerome.bignumber=${random.long}
#jerome.number.less.than.ten=${random.int(10)}
#jerome.number.in.range=${random.int[1024,65536]}
jerome.random.int=${random.int[1024,65536]}
#自定义名字
#jerome.name=www.jerome.com
#属性占位符属性
jerome.desc=${jerome.name} is a domain name
代码使用
// 32位随机字符串
@Value(value = "${jerome.random.value}")
private String randomValue;

// int类型的随机数字
@Value(value = "${jerome.random.int}")
private int randomInt;

// 自定义名字
@Value(value = "${jerome.name}")
private String name;

// 属性占位符属性 ${jerome.name} is a domain name
@Value(value = "${jerome.desc}")
private String desc;
/**
* 获得配置文件的数据
*
* @return Json
*/
@RequestMapping("testProp")
Map<String, Object> testProp() {
Map<String, Object> map = new HashMap<>();
map.put("title", "hello world");
map.put("randomValue", randomValue);
map.put("randomInt", randomInt);
map.put("name", name);
map.put("desc", desc);
return map;
}
返回结果
    

使用YAML代替Properties(如果Properties和YAML都存在,已Properties为准

    编写注意:冒号后面要加空格,多级使用tab缩进。
application.yaml
#自定义配置
jerome:
# secret: ${random.value}
# number: ${random.int}
name: www.suzhida.com
desc: ${roncoo.name} is a domain name

#端口
server:
port: 80

#spring jsckson
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Chongqing

多环境配置(开发环境、测试坏境、生产环境)

多环境配置的好处

        1. 不同环境配置可以配置不同的参数
        2. 便于部署,提高效力,减少出错

Properties多环境配置

        1. 主配置激活选项
            spring.profiles.active=dev
        2.添加其他配置文件
         
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生