配置文件
1 2 3 4 5 6 7 8 9 10 11 12
| person: lastName: hello age: 18 boss: false birth: 2017/12/12 maps: {k1: v1,k2: 12} lists: - lisi - wanglei dog: name: 小狗 age: 12
|
javaBean:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
@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;
|
我们可以导入配置文件处理器,以后编写配置就有提示了
1 2 3 4 5 6
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
|
1、properties配置文件在idea中默认utf-8可能会乱码
调整

2、@Value获取值和@ConfigurationProperties获取值比较
|
@ConfigurationProperties |
@Value |
功能 |
批量注入配置文件中的属性 |
一个个指定 |
松散绑定(松散语法) |
支持 |
不支持 |
SpEL |
不支持 |
支持 |
JSR303数据校验 |
支持 |
不支持 |
复杂类型封装 |
支持 |
不支持 |
配置文件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;
3、配置文件注入值数据校验
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @Component @ConfigurationProperties(prefix = "person") @Validated public class Person {
@Email private String lastName; private Integer age; private Boolean boss;
private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog;
|
4、@PropertySource&@ImportResource&@Bean
@PropertySource:加载指定的配置文件;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
@PropertySource(value = {"classpath:person.properties"}) @Component @ConfigurationProperties(prefix = "person")
public class Person {
private String lastName; private Integer age; private Boolean boss;
|
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上
1 2
| @ImportResource(locations = {"classpath:beans.xml"}) 导入Spring的配置文件让其生效
|
不来编写Spring的配置文件
1 2 3 4 5 6 7 8
| <?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.atguigu.springboot.service.HelloService"></bean> </beans>
|
SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式
1、配置类@Configuration——>Spring配置文件
2、使用@Bean给容器中添加组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
@Configuration public class MyAppConfig {
@Bean public HelloService helloService02(){ System.out.println("配置类@Bean给容器中添加组件了..."); return new HelloService(); } }
|
##4、配置文件占位符
1、随机数
1 2
| ${random.value}、${random.int}、${random.long} ${random.int(10)}、${random.int[1024,65536]}
|
2、占位符获取之前配置的值,如果没有可以是用:指定默认值
1 2 3 4 5 6 7 8 9
| person.last-name=王蕾${random.uuid} person.age=${random.int} person.birth=2017/12/15 person.boss=false person.maps.k1=v1 person.maps.k2=14 person.lists=a,b,c person.dog.name=${person.hello:hello}_dog person.dog.age=15
|