@ConfigurationProperties用法及說(shuō)明
@ConfigurationProperties用法
@ConfigurationProperties功能類似于@Value
都可以用來(lái)獲取配置文件中的數(shù)據(jù)
@ConfigurationProperties 只需要在實(shí)體類上添加一個(gè)注解,
通過(guò)屬性名和配置文件的中的名字對(duì)照(實(shí)體類的屬性名和配置文件中的名稱要相同,
若配置文件中是card-id形式寫法
在實(shí)體類的就需要寫成駝峰形式,否則會(huì)獲取不到),進(jìn)行綁定。
# 配置文件
spring:
redis:
# redis服務(wù)器地址
host: 127.0.0.1
# 端口
port: 6379
# 密碼
password: 123456
# 默認(rèn)為0庫(kù)
database: 2
# 連接超時(shí)時(shí)間
timeout: 10000ms
lettuce:
pool:
# 最大連接數(shù),默認(rèn)8
maxActive: 1024
# 最大連接阻塞等待時(shí)間,單位毫秒,默認(rèn)-1ms
maxWait: 10000ms
# 最大空閑連接,默認(rèn)8
maxIdle: 200
# 最小空閑連接,默認(rèn)0
minIdle: 5// 通過(guò)對(duì)比前綴是 spring.redis.lettuce.pool 內(nèi)容
// 與pojo對(duì)象的屬性比較并進(jìn)行綁定
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
// 一定此注解,添加到容器中,使用的時(shí)候通過(guò)自動(dòng)裝配引入即可
@Component
public class Lettuce {
private Integer maxActive;
private String maxWait;
private Integer maxIdle;
private Integer minIdle;
public Integer getMaxActive() {
return maxActive;
}
public void setMaxActive(Integer maxActive) {
this.maxActive = maxActive;
}
public String getMaxWait() {
return maxWait;
}
public void setMaxWait(String maxWait) {
this.maxWait = maxWait;
}
public Integer getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(Integer maxIdle) {
this.maxIdle = maxIdle;
}
public Integer getMinIdle() {
return minIdle;
}
public void setMinIdle(Integer minIdle) {
this.minIdle = minIdle;
}
public Lettuce(Integer maxActive, String maxWait,Integer maxIdle,Integer minIdle){
this.maxActive = maxActive;
this.maxWait = maxWait;
this.maxIdle = maxIdle;
this.minIdle = minIdle;
}
public Lettuce(){
}
@Override
public String toString() {
return "Lettuce{" +
"maxActive=" + maxActive +
", maxWait='" + maxWait + '\'' +
", maxIdle=" + maxIdle +
", minIdle=" + minIdle +
'}';
}
}
@SpringBootTest
class SpringdataDemoApplicationTests {
@Autowired
private Lettuce lettuce;`
@Test
public void test(){
System.out.println(lettuce.toString());
}
}``
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 深入解析Spring Boot中的@ConfigurationProperties注解
- @ConfigurationProperties及@NestedConfigurationProperty的使用解讀
- 將SpringBoot屬性配置類@ConfigurationProperties注冊(cè)為Bean的操作方法
- SpringBoot @ConfigurationProperties + Validation實(shí)現(xiàn)啟動(dòng)期校驗(yàn)解決方案
- 解讀@ConfigurationProperties和@value的區(qū)別
- springboot使用@ConfigurationProperties實(shí)現(xiàn)自動(dòng)綁定配置參數(shù)屬性
- 解讀@ConfigurationProperties的基本用法
相關(guān)文章
Spring接口版本控制方案及RequestMappingHandlerMapping接口介紹(最新推薦)
RequestMappingHandlerMapping接口是Spring MVC中的一個(gè)核心組件,負(fù)責(zé)處理請(qǐng)求映射和處理器的匹配這篇文章主要介紹了Spring接口版本控制方案及RequestMappingHandlerMapping接口介紹,需要的朋友可以參考下2024-07-07
MyBatis Mapper.xml入?yún)ist使用in函數(shù)問(wèn)題
文章主要講述了在使用MyBatis的Mapper.xml文件時(shí),如何正確地在in函數(shù)中使用List作為入?yún)?作者強(qiáng)調(diào)了完整拷貝<if>...</if>格式的重要性,并指出稍微的改動(dòng)就會(huì)導(dǎo)致錯(cuò)誤2025-02-02
mybatis使用foreach查詢不出結(jié)果也不報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了mybatis使用foreach查詢不出結(jié)果也不報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
詳解Java的TCP/IP編程學(xué)習(xí)--基于定界符的成幀
這篇文章主要介紹了Java的TCP/IP編程學(xué)習(xí)--基于定界符的成幀,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

