前言
我們知道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文件,配置對應的屬性
student.name=kenx
student.age=23
- 新增java bean 把對應的屬性注入到java bean中對應字段使用@Value注解將屬性值注入到對應屬性上。
@Component
@Data
public class User {
@Value("${student.name}")
private String name;
@Value("${student.age}")
private Integer age;
}
@Component 添加到spring ioc容器中,@Data 添加getter,setter
- 寫用例測試
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.NONE,
classes = cn.soboys.kmall.api.ApiApplication.class)
public class PropertiesTest {
@Autowired
private User properties;
@Test
public void a(){
String a= String.format( "student name is %s student age is %s",properties.getName(),properties.getAge());
System.out.println(a);
}
}
我看可以看到控制台正常打印,數據注入成功
2021-09-08 10:53:02 INFO background-preinit org.hibernate.validator.internal.util.Version HV000001: Hibernate Validator 6.1.7.Final
2021-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)
2021-09-08 10:53:02 INFO main PropertiesTest The following profiles are active: test,mptest
_ _ |_ _ _|_. ___ _ | _
| | |\/|_)(_| | |_\ |_)||_|_\
/ |
3.4.1
2021-09-08 10:53:08 INFO main PropertiesTest Started PropertiesTest in 6.132 seconds (JVM running for 7.783)
student name is kenx student age is 23
@ConfigurationProperties
注解將屬性值綁定到結構化的beans
上面通過@Value
一個·一個注入很不方便
@Component
@Data
@ConfigurationProperties(prefix = "student")
public class User {
private String name;
private Integer age;
}
這樣極大簡化代碼,對於屬性比較多,結構化bean,很有必要可以通過
@ConfigurationProperties(prefix = "student")
這種方式指定前綴
當然有時候我們需要自定義加載屬性配置文件 使用@PropertySource
加載配置文件
test.id=100
test.name=lucy
package com.lzx.springboot01demo.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration // 自定義配置類
@PropertySource("classpath:test.properties") // 指定自定義配置文件位置和名稱
@EnableConfigurationProperties(MyProperties.class) // 開啟對應配置類的屬性注入功能
@ConfigurationProperties(prefix = "test") // 指定配置文件注入屬性前綴
public class MyProperties {
private Integer id;
private String name;
// 省略getter/setter方法
// 省略toString()方法
}
1. @Configuration
注解表示當前類是一個自定義配置類,並添加為Spring容器的組件,也可使用傳統的@Component
注解
-
@PropertySource("classpath:test.properties")
指定自定義配置文件位置和名稱 -
@ConfigurationProperties(prefix = "test")
指定將配置文件中前綴為test的屬性注入到配置類的屬性中 -
@EnableConfigurationProperties(MyProperties.class)
表示開啟對應配置類的屬性注入功能,如果配置類上使用的是@Component注解而非@Configuration,@EnableConfigurationProperties(MyProperties.class)注解可以省略
application.properties配置文件
#配置數字
person.id=1
#配置字符串
person.name=tom
#配置List集合
person.hoby=吃飯,睡覺,打豆豆
#配置String[]數組
person.family=father,mother
#配置map集合
person.map.k1=v1
person.map.k2=v2
#配置對象type屬性
person.pet.type=dog
#配置對象name屬性
person.pet.name=旺財
application.y(a)ml配置文件
- value值為普通數據類型(例如:數字、字符串、布爾)
server:
port: 8081
path: /hello
- value值為數組或單列集合
主要有兩種寫法:縮進式寫法和行內式寫法;其中縮進式寫法又有兩種寫法:
縮進式寫法1
person:
hobby:
- play
- read
- sleep
縮進式寫法2
person:
hobby:
play,
read,
sleep
行內式寫法:
person:
hobby: [play,read,sleep]
- value值為Map或對象
縮進式寫法
person:
map:
k1: v1
k2: v2
行內式寫法:
person:
map: {k1: v1, k2: v2}
注意 使用Spring Boot全局配置文件設置屬性時,
如果配置的屬性是已有屬性,例如服務端口server.port,那么Spring Boot會掃描並讀取這些配置屬性,覆蓋
已有的默認配置;
如果配置的是自定義屬性,則還需要在程序中注入
這些配置屬性方可生效
默認屬性和參數引用
SpringBoot
屬性配置文件中默認給我們提供了一些特有的全局屬性參數值我們可以直接獲取
使用Spring Boot內嵌的RandomValuePropertySource類進行隨機值注入。
# 配置隨機值
my.secret=${random.value}
# 配置隨機整數
my.number=${random.int}
# 配置隨機long類型的整數
my.bigbumber=${random.long}
# 配置uuid
my.uuid=${random.uuid}
# 配置小於10的整數
my.number.less.than.ten=${random.int(10)}
# 配置范圍在[1024,65536]的隨機整數
my.number.in.range=${random.int[1024,65536]}
當然我們也可以自定義引用自己定義的值
# 參數間引用
app.name=MyApp
app.description=${app.name} is a Spring Boot application