前言
我們知道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