Spring Boot 配置文件介绍
来源:互联网 发布:java成绩管理系统代码 编辑:程序博客网 时间:2024/06/14 15:37
Spring Boot 配置文件介绍
2017-01-10 20:56 712人阅读 评论(0)收藏举报
分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
属性文件使用Application/YAML
Application/YAML 属性文件,按优先级按高到低排序,位置高的将覆盖位置低的
1. 当前目录下的一个/config子目录
2. 当前目录
3. 一个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.添加其他配置文件
YAML多环境配置
#配置文件环境配置
spring:
profiles:
active: dev
#jackson
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Chongqing
#自定义配置
jerome:
# secret: ${random.value}
# number: ${random.int}
name: www.suzhida.com
desc: ${roncoo.name} is a domain name
#端口
server:
port: 8888
---
spring:
profiles: dev
server:
port: 8080
---
spring:
profiles: prod
server:
port: 8082
---
spring:
profiles: test
server:
port: 8081
两种配置方式的比较
1. Properties配置多环境,需要添加多个配置文件,YAML只需要一个配件文件
2.书写格式的差异,yaml相对比较简洁,优雅
3. YAML的缺点:不能通过@PropertySource注解加载。如果需要使用@PropertySource注解的方式加载值,那就要使用properties文件。
使用
java -jar demo_spring_boot-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
阅读全文
0 0
- Spring Boot 配置文件介绍
- Spring Boot 配置文件介绍
- Spring Boot配置文件简单介绍
- spring-boot配置文件
- Spring Boot 配置文件模板
- Spring Boot属性配置文件
- spring boot 工程配置文件
- Spring boot 配置文件
- Spring boot读取配置文件
- spring boot配置文件
- spring boot (二) 配置文件
- spring-boot 配置文件
- spring Boot 配置文件详解
- Spring Boot之配置文件
- spring-boot 配置文件读取
- Spring boot 配置文件读取
- spring boot配置文件
- spring boot 读取配置文件
- 有关jQuery Angular面试题
- [LeetCode] 040: Largest Rectangle in Histogram
- 机房重构中的sqlHelper;Datatable;SQLParameter;SQLCommand都是什么
- [LeetCode] 041: Length of Last Word
- [LeetCode] 042: Letter Combinations of a Phone Number
- Spring Boot 配置文件介绍
- [LeetCode] 043: Longest Common Prefix
- [LeetCode] 044: Longest Consecutive Sequence
- [LeetCode] 045: Longest Palindromic Substring
- [LeetCode] 046: Longest Substring Without Repeating Characters
- [LeetCode] 047: Longest Valid Parentheses
- [LeetCode] 048: Maximal Rectangle
- asynctask_httpclient_02
- [LeetCode] 049: Maximum Depth of Binary Tree