Spring Boot 默认全局配置文件,分别是properties文件和yml文件。他们主要作用是修改Spring Boot的自动配置的默认值,相对于properties文件而言,更多人喜欢使用yml的配置文件。在此之前我们一般都是使用xml的形式来完成我们的配置。xml是以标签的形式存在,而properties则是k=v的形式存在,相对于前两者而言, yml是以数据为中心,比json、xml更适合做配置文件,yml的出现更受大众的青睐。本文重点说说yml的魅力之处。以前我们都觉得配置就是一个项目的核心,现在只要记住一句话就OK啦!“约定大于配置”
一、.xml的语法,是一个标记文档(以标签的形式存在,写起来比较繁琐)
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd ">
<!-- 开启注解 -->
<context:annotation-config />
<!-- 开启定时任务 -->
<task:annotation-driven/>
<!-- 自动扫描(service),mapper(dao) -->
<context:component-scan base-package="com.gx.mapper,com.gx.service,com.gx.job">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>
</beans>
二、.properties的语法 (以k=v的形式存在,写起来比上面的xml好不了多少)
//修改默认的端口号
server.port=8081
//访问静态资源的路径
spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources
//文件上传的最大值
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.max-file-size=10MB
三、.yml 的语法,YAML A Markup Language:是一个标记语言 (以K空格V 的形式存在,写起来比较简便)
“约定大于配置”,我想这句话很多人都听说了吧!其实这么说也有它的道理的。
1、基本语法:
(1)、属性和值的大小字母都是比较敏感;
(2)、k:空格v:表示一对键值对(空格必须要有);
(3)、以空格的缩进来控制控制层级关系;只要是左对齐的一列数据,都是同一个级;否则会出现数据混乱
server:
port: 8090
path: /hello
2、值的写法:
(1)、在字符串的赋值时,不需要添加单引号或者双引号。如果里添加了单(双)引号,则特殊字符以字符串的形式输出。字符串默认不用加上单引号或者双引号。
正确 :name: 小明 \n 小红:输出:小明 换行 小红
错误:name: ‘小明 \n 小红’:输出;小明\n 小红
3、对象、Map的写法:(对象是以属性和值,Map是键值对)
//第一种写法
Friends: {lastName: xiaozhang, age: 20}
//第二中写法。在写的时候需要注意:在下一行来写对象和属性的关系,记得缩进 。对象是以K:(空格)V的形式
friends:
lastName: xiaozhang
Age: 20
4、数组(List)的写法
//-(空格)值表示数组中的某一个的元素
Pets:
- cat
-dog
-pig
5、以学生类为例
Student类
@Component //将此Javabean放入spring容器
@ConfigurationProperties(prefix="student") //通过他批量注入
public class Student {
@Value(“ls”) //修改其中的值
private String name;/* 姓名 */
@Value(“33”)
private int age;/* 年龄 */
private boolean sex;/* 性别 */
private Date birthday;/* 出生日期 */
private Map<String, Object> location; /* 家庭地址 */
private String[] hobbies; /* 爱好 */
private List<String> skills; /* 技能 */
private Pet pet;/* 宠物 *
//生成get和set
//生成toString()
}
yml赋值
student:
name: ls
age: 24
sex: true
birthday: 2019/2/12
location: {province: 广东,city: 广州,zone: 天河区}
hobbies:
- 篮球
- 音乐
skills:
- 编程
- 金融
pet:
nickName: wangcai
strain: hsq
第一次输出的结果:
修改后输出结果:
通过上面的结论:一次又一次证明了.yml的配置,符合了约定大于配置。如果里的写法不符合约定就会出错。虽然.yml的配置写法确实减少了不少,但我们一定要遵守我们的约定。