前言
我们知道SpringBoot 通过配置类来解放一堆的xml文件配置,通属性配置文件,来进行,系统全局属性配置,这样极大的简化了我们开发过程,java web 也可以甜甜的从此

快速配置
Spring Boot默认加载支持 application*.properties、application*.yaml和application*.yml三种拓展名结尾的全局属性配置文件处理
它们顺序优先级为: application*.properties>application*.yaml>application*.yml
即在application.properties或application.yml等文件中添加属性配置
可以使用@Value注解将属性值注入到beans中,或使用@ConfigurationProperties注解将属性值绑定到结构化的beans中
@Value是Spring框架提供的注解,用来读取配置文件中的属性并逐个注入到Bean对象对应的属性中,Spring Boot框架对Spring框架的@Value注解进行了默认继承
- 在
resources文件下新增application.properties文件,配置对应的属性
1student.name=kenx 2student.age=23
- 新增java bean 把对应的属性注入到java bean中对应字段使用@Value注解将属性值注入到对应属性上。
1@Component 2@Data 3public class User { 4 @Value("${student.name}") 5 private String name; 6 @Value("${student.age}") 7 private Integer age; 8}
@Component 添加到spring ioc容器中,@Data 添加getter,setter
- 写用例测试
1@SpringBootTest( 2 webEnvironment = SpringBootTest.WebEnvironment.NONE, 3 classes = cn.soboys.kmall.api.ApiApplication.class) 4public class PropertiesTest { 5 6 @Autowired 7 private User properties; 8 9 @Test 10 public void a(){ 11 String a= String.format( "student name is %s student age is %s",properties.getName(),properties.getAge()); 12 System.out.println(a); 13 } 14}
我看可以看到控制台正常打印,数据注入成功
12021-09-08 10:53:02 INFO background-preinit org.hibernate.validator.internal.util.Version HV000001: Hibernate Validator 6.1.7.Final 22021-09-08 10:53:02 INFO main PropertiesTest Starting PropertiesTest using Java 1.8.0_202 on xiangyongdeMacBook-Pro.local with PID 45463 (started by xiangyong in /Users/xiangyong/selfProject/project/kmall/kmall-api) 32021-09-08 10:53:02 INFO main PropertiesTest The following profiles are active: test,mptest 4 _ _ |_ _ _|_. ___ _ | _ 5| | |\/|_)(_| | |_\ |_)||_|_\ 6 / | 7 3.4.1 82021-09-08 10:53:08 INFO main PropertiesTest Started PropertiesTest in 6.132 seconds (JVM running for 7.783) 9 10student name is kenx student age is 23
@ConfigurationProperties注解将属性值绑定到结构化的beans
上面通过@Value一个·一个注入很不方便
1@Component 2@Data 3@ConfigurationProperties(prefix = "student") 4public class User { 5 private String name; 6 private Integer age; 7}
这样极大简化代码,对于属性比较多,结构化bean,很有必要可以通过
@ConfigurationProperties(prefix = "student")这种方式指定前缀
当然有时候我们需要自定义加载属性配置文件 使用@PropertySource加载配置文件
1test.id=100 2test.name=lucy 3
1package com.lzx.springboot01demo.pojo; 2 3import org.springframework.boot.context.properties.ConfigurationProperties; 4import org.springframework.boot.context.properties.EnableConfigurationProperties; 5import org.springframework.context.annotation.Configuration; 6import org.springframework.context.annotation.PropertySource; 7 8@Configuration // 自定义配置类 9@PropertySource("classpath:test.properties") // 指定自定义配置文件位置和名称 10@EnableConfigurationProperties(MyProperties.class) // 开启对应配置类的属性注入功能 11@ConfigurationProperties(prefix = "test") // 指定配置文件注入属性前缀 12public class MyProperties { 13 14 private Integer id; 15 private String name; 16 17 // 省略getter/setter方法 18 // 省略toString()方法 19} 20
1. @Configuration注解表示当前类是一个自定义配置类,并添加为Spring容器的组件,也可使用传统的@Component注解
-
@PropertySource("classpath:test.properties")指定自定义配置文件位置和名称 -
@ConfigurationProperties(prefix = "test")指定将配置文件中前缀为test的属性注入到配置类的属性中 -
@EnableConfigurationProperties(MyProperties.class)表示开启对应配置类的属性注入功能,如果配置类上使用的是@Component注解而非@Configuration,@EnableConfigurationProperties(MyProperties.class)注解可以省略
application.properties配置文件
1#配置数字 2person.id=1 3#配置字符串 4person.name=tom 5#配置List集合 6person.hoby=吃饭,睡觉,打豆豆 7#配置String[]数组 8person.family=father,mother 9#配置map集合 10person.map.k1=v1 11person.map.k2=v2 12#配置对象type属性 13person.pet.type=dog 14#配置对象name属性 15person.pet.name=旺财
application.y(a)ml配置文件
- value值为普通数据类型(例如:数字、字符串、布尔)
1server: 2 port: 8081 3 path: /hello 4
- value值为数组或单列集合
主要有两种写法:缩进式写法和行内式写法;其中缩进式写法又有两种写法:
缩进式写法1
1person: 2 hobby: 3 - play 4 - read 5 - sleep
缩进式写法2
1person: 2 hobby: 3 play, 4 read, 5 sleep 6
行内式写法:
1person: 2 hobby: [play,read,sleep] 3
- value值为Map或对象
缩进式写法
1person: 2 map: 3 k1: v1 4 k2: v2 5
行内式写法:
1person: 2 map: {k1: v1, k2: v2} 3
注意 使用Spring Boot全局配置文件设置属性时,
如果配置的属性是已有属性,例如服务端口server.port,那么Spring Boot会扫描并读取这些配置属性,覆盖已有的默认配置;
如果配置的是自定义属性,则还需要在程序中注入这些配置属性方可生效
默认属性和参数引用
SpringBoot属性配置文件中默认给我们提供了一些特有的全局属性参数值我们可以直接获取
使用Spring Boot内嵌的RandomValuePropertySource类进行随机值注入。
1# 配置随机值 2my.secret=${random.value} 3# 配置随机整数 4my.number=${random.int} 5# 配置随机long类型的整数 6my.bigbumber=${random.long} 7# 配置uuid 8my.uuid=${random.uuid} 9# 配置小于10的整数 10my.number.less.than.ten=${random.int(10)} 11# 配置范围在[1024,65536]的随机整数 12my.number.in.range=${random.int[1024,65536]}
当然我们也可以自定义引用自己定义的值
1# 参数间引用 2app.name=MyApp 3app.description=${app.name} is a Spring Boot application 4
