https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config
两个全局配置文件
SpringBoot是基于约定的,所以很多配置都有默认值,如果想要用自己的配置替换默认的配置,,就可以使用application.properties或者application.yml(application.yaml)进行配置。
SpringBoot默认会从Resources目录下加载application.properties或application.yml(application.yaml)【名称是固定的】文件
语法 配置普通数据 语法: key: value 实例: name: haohao 注意:value之前有一个空格 配置对象/map数据 语法: key: key1: value1key2: value2或者:key: {key1: value1,key2: value2} 实例: person: name: haohao age: 31 addr: beijing#或者person: {name: haohao,age: 31,addr: beijing} 注意:key1前面的空格个数不限定,在yml语法中,相同缩进代表同一个级别 配置数组(List、Set)数据 语法 key: - value1- value2或者:key: [value1,value2] 实例 city: - beijing - tianjin - shanghai - chongqing #或者city: [beijing,tianjin,shanghai,chongqing]#集合中的元素是对象形式student: - name: zhangsan age: 18 score: 100 - name: lisi age: 28 score: 88 - name: wangwu age: 38 score: 90 注意:value1与之间的 - 之间存在一个空格 两种写法 yml配置文件值注入
1、导入配置文件处理器,配置文件进行绑定会有提示
pom.xml中
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.2.2.RELEASE</version> <optional>true</optional> </dependency>
2、 javaBean
package com.oceanstar.quick_springboot.bean;public class Dog { private String name; private Integer age; @Component@ConfigurationProperties(prefix = "person")public class Person { private String lastName; private Integer age; private Boolean boss; private Date birth; private Map<String, Object> maps; private List<Object> lists; private Dog dog;
3、 配置文件
application.yml
person: lastName: hello age: 18 boss: false birth: 2018/01/10 maps: {k1:v1, k2:12} lists: - lisi - zhangs dog: name: 小狗 age: 12
4、编写测试文件
D:workspacequick_springbootsrctestjavacomoceanstarquick_springbootQuickSpringbootApplicationTests.java
@SpringBootTestclass QuickSpringbootApplicationTests { @Autowired Person person; @Test void contextLoads() { System.out.println(person); }}
properties配置
1、去掉yml中的配置
2、application.properties中
person.age=11person.birth=2018/11/12person.boss=falseperson.last-name=开朗的裙子person.maps.k1=v1person.maps.k2=v2person.lists=a,b,vperson.dog.name=zzdwsperson.dog.age=1
3、如果出现中文乱码,需要设置编码为utf-8,并且转为ascii运行:
File----》Settings-----》Editor----》File Encodings----》
两种注入方式 @ConfigurationProperties(prefix = “person”)
@Value
比较
如果只需要在某个业务逻辑中获取一下配置文件中的值,使用@Value
如果专门编写了一个javaBean和配置文件进行映射,就直接使用@ConfigurationProperties
注意:使用@ConfigurationProperties方式可以进行配置文件与实体字段的自动映射,但需要字段必须提供set方法才可以,而使用@Value注解修饰的字段不需要提供set方法
配置文件注入数据校验
其他配置文件 @PropertySource
作用:指定特定配置文件
内容
person.age=11person.birth=2018/11/12person.boss=falseperson.last-name=开朗的裙子person.maps.k1=v1person.maps.k2=v2person.lists=a,b,vperson.dog.name=mhdqzperson.dog.age=1
@PropertySource(value = {“classpath:person.properties”}): 告知ConfigurationProperties从person.properties获取值
@ImportResource 我们自己编写的配置文件,SpringBoot是不认的,如果想要SpringBoot自己去读取自定义的配置文件,用 @ImportResource但是这个注解很少用,因为Spring推荐使用全注解的方式给容器中添加组件,参见@Configution和@Bean
1、准备一个class
package com.oceanstar.quick_springboot.service;public class HelloService {}
2、自定义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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloService" class="com.oceanstar.quick_springboot.service.HelloService"></bean></beans>
3、编写测试文件
@SpringBootTestclass QuickSpringbootApplicationTests { @Autowired ApplicationContext ioc; @Test void contextLoads() { System.out.println(ioc.containsBean("helloService")); }}
4、在主配置类上加上注解
@ImportResource(locations = {"classpath:bean.xml"})@SpringBootApplicationpublic class QuickSpringbootApplication {
5、重新运行测试文件
@Configuration和 @Bean package com.oceanstar.quick_springboot.config;import com.oceanstar.quick_springboot.service.HelloService;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration // 这是一个配置类,用来替代原来的bean.xmlpublic class MyAppConfig { @Bean //将方法的返回值添加的容器中;容器中这个组件默认的id就是方法名gethellServiceBeans public HelloService gethellServiceBeans(){ return new HelloService(); }}
测试文件
@SpringBootTestclass QuickSpringbootApplicationTests { @Autowired ApplicationContext ioc; @Test void contextLoads() { System.out.println(ioc.containsBean("gethellServiceBeans")); }} 占位符
Profile多配置
Profile是spring对不同环境提供不同配置功能的支持
多profile文件格式
格式:application-{xxxx}.properties或者 application-{xxxx}.yml
默认作用的是application.properties
可以通过在配置文件中:spring.profiles.active=xxx激活指定application-{xxxx}.properties文件
多文档快格式
多激活方式
配置文件加载位置 四个位置
• spring boot 启动会扫描以下位置的application.properties或者
application.yml文件作为Spring boot的默认配置文件
– file:./config/
– file:./
– classpath:/config/
– classpath:/
– 以上是按照优先级从高到低的顺序,所有位置的文件都会被加载,高优先
级配置内容会覆盖低优先级配置内容。
SpringBoot会从这四个位置全部加载主配置文件,互补配置
1和2的位置不推荐使用,因为不会被打包
命令行指定默认配置文件位置
项目打包好之后,可以使用命令行参数spring.config.location指定配置文件的新位置,指定配置文件和默认加载的配置文件形成互补配置