springboot如何通過@Value,@ConfigurationProperties獲取配置
通過@Value,@ConfigurationProperties獲取配置
spring boot 獲取配置項(xiàng)值
使用版本是1.5.4
舉例一個線程池的配置:
在application.yml添加配置項(xiàng)及值
? ? # 線程池配置 ? ? taskexecutor: ? ? ? corePoolSize: 5 ? ? ? maxPoolSize: 10 ? ? ? queueCapacity: 25
通過@Value 獲取值
@Configuration
@EnableAsync
public class ExecutorConfig {
? ? @Value("${taskexecutor.corePoolSize}")
? ? private int corePoolSize;
? ? @Value("${taskexecutor.maxPoolSize}")
? ? private int maxPoolSize;
? ? @Value("${taskexecutor.queueCapacity}")
? ? private int queueCapacity;
? ? @Bean
? ? public Executor getAsyncExecutor() {
? ? ? ? ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
? ? ? ? executor.setCorePoolSize(corePoolSize);
? ? ? ? executor.setMaxPoolSize(maxPoolSize);
? ? ? ? executor.setQueueCapacity(queueCapacity);
? ? ? ? executor.setThreadNamePrefix("TaskExecutor-");
? ? ? ? executor.initialize();
? ? ? ? return executor;
? ? }
? ? }通過@ConfigurationProperties 獲取值
? ? @Configuration
? ? @EnableAsync
? ? @ConfigurationProperties(ignoreUnknownFields = false,prefix = "taskexecutor")
? ? public class ExecutorConfig {
? ? private int corePoolSize;
? ? private int maxPoolSize;
? ? private int queueCapacity;
? ? @Bean
? ? public Executor getAsyncExecutor() {
? ? ? ? ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
? ? ? ? executor.setCorePoolSize(corePoolSize);
? ? ? ? executor.setMaxPoolSize(maxPoolSize);
? ? ? ? executor.setQueueCapacity(queueCapacity);
? ? ? ? executor.setThreadNamePrefix("TaskExecutor-");
? ? ? ? executor.initialize();
? ? ? ? return executor;
? ? }
? ? }通過@ConfigurationProperties加載配置文件,將配置項(xiàng)與bean及屬性關(guān)聯(lián),指定ignoreUnknownFields當(dāng)有屬性未匹配到值時(shí)會拋出異常,用prefix指定配置項(xiàng)的前綴。
@ConfigurationProperties還支持層級結(jié)構(gòu)、 布爾、集合等類型的值注入
說下@ConfigurationProperties和@Value區(qū)別
| @Configuration | @Value | |
| 功能 | 批量注入配置文件中的屬性 | 一個個指定 |
| 松散綁定(松散語法) | 支持 | 不支持 |
| SPEL語法 | 不支持 | 支持 |
| JSR303數(shù)據(jù)校驗(yàn) | 支持 | 不支持 |
| 復(fù)雜類型封裝 | 支持 | 不支持 |
配置文件yml還是properties他們都能獲取到值;
如果說, 只是在某個業(yè)務(wù)邏輯中需要獲取一項(xiàng)配置文件中的某項(xiàng)值, 使用@Value
如果說,專門編寫了一個javaBean 來和配置文件進(jìn)行映射,我們就直接使用@ConfigurationProperties;
配置文件注入值數(shù)據(jù)校驗(yàn)
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
/**
* <bean class="Person">
* <property name="lastName" value="字面值/${key} 從環(huán)境變量,配置文件中獲取值/#{Spel}"></property>
* </bean>
*/
//Value("${person.last-name}")
//lastName必須為郵箱格式
@Email
private String lastName;
//@Value("#{11*2}")
private Integer age;
//@Value("true")
private Boolean boss;
private Date birth;
private Map<String, Object> maps;
private List<Object> list;
private Dog dog;
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot在容器中創(chuàng)建實(shí)例@Component和@bean有什么區(qū)別
這篇文章主要介紹了SpringBoot在容器中創(chuàng)建實(shí)例@Component和@bean有什么區(qū)別,在Spring Boot中,@Component注解和@Bean注解都可以用于創(chuàng)建bean。它們的主要區(qū)別在于它們的作用范圍和創(chuàng)建方式2023-03-03
itext生成PDF設(shè)置頁眉頁腳的實(shí)例詳解
這篇文章主要介紹了itext生成PDF設(shè)置頁眉頁腳的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
詳解Spring Boot中使用Flyway來管理數(shù)據(jù)庫版本
這篇文章主要介紹了詳解Spring Boot中使用Flyway來管理數(shù)據(jù)庫版本,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
mybatis foreach批量插入數(shù)據(jù):Oracle與MySQL區(qū)別介紹
這篇文章主要介紹了,需要的朋友可以參考下2018-01-01
mybatis plus in使用時(shí)傳數(shù)組、集合的注意點(diǎn)說明
這篇文章主要介紹了mybatis plus in使用時(shí)傳數(shù)組、集合的注意點(diǎn)說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Java創(chuàng)建可執(zhí)行的Jar文件的方法實(shí)踐
創(chuàng)建的可執(zhí)行Jar文件實(shí)際就是在原始Jar的清單文件中添加了Main-Class的配置,本文主要介紹了Java創(chuàng)建可執(zhí)行的Jar文件的方法實(shí)踐,感興趣的可以了解一下2023-12-12
簡單談?wù)凾hreadPoolExecutor線程池之submit方法
下面小編就為大家?guī)硪黄唵握務(wù)凾hreadPoolExecutor線程池之submit方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
SpringBoot?SpringSecurity?JWT實(shí)現(xiàn)系統(tǒng)安全策略詳解
Spring?Security是Spring的一個核心項(xiàng)目,它是一個功能強(qiáng)大且高度可定制的認(rèn)證和訪問控制框架。它提供了認(rèn)證和授權(quán)功能以及抵御常見的攻擊,它已經(jīng)成為保護(hù)基于spring的應(yīng)用程序的事實(shí)標(biāo)準(zhǔn)2022-11-11

