springboot如何通過@Value,@ConfigurationProperties獲取配置
通過@Value,@ConfigurationProperties獲取配置
spring boot 獲取配置項(xiàng)值
使用版本是1.5.4
舉例一個(gè)線程池的配置:
在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í)會(huì)拋出異常,用prefix指定配置項(xiàng)的前綴。
@ConfigurationProperties還支持層級(jí)結(jié)構(gòu)、 布爾、集合等類型的值注入
說下@ConfigurationProperties和@Value區(qū)別
| @Configuration | @Value | |
| 功能 | 批量注入配置文件中的屬性 | 一個(gè)個(gè)指定 |
| 松散綁定(松散語法) | 支持 | 不支持 |
| SPEL語法 | 不支持 | 支持 |
| JSR303數(shù)據(jù)校驗(yàn) | 支持 | 不支持 |
| 復(fù)雜類型封裝 | 支持 | 不支持 |
配置文件yml還是properties他們都能獲取到值;
如果說, 只是在某個(gè)業(yè)務(wù)邏輯中需要獲取一項(xiàng)配置文件中的某項(xiàng)值, 使用@Value
如果說,專門編寫了一個(gè)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;
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
第一次使用Android Studio時(shí)你應(yīng)該知道的一切配置(推薦)
這篇文章主要介紹了第一次使用Android Studio時(shí)你應(yīng)該知道的一切配置(推薦) ,需要的朋友可以參考下2017-09-09
使用httpclient實(shí)現(xiàn)免費(fèi)的google翻譯api
這篇文章主要介紹了使用httpclient實(shí)現(xiàn)免費(fèi)的google翻譯api的方法,大家參考使用吧2014-01-01
spring常用注解開發(fā)一個(gè)RESTful接口示例
這篇文章主要為大家介紹了使用spring常用注解開發(fā)一個(gè)RESTful接口示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
PowerJob的TransportServiceAware工作流程源碼解讀
這篇文章主要介紹了PowerJob的TransportServiceAware工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
SpringBoot+Vue跨域配置(CORS)問題得解決過程
在使用 Spring Boot 和 Vue 開發(fā)前后端分離的項(xiàng)目時(shí),跨域資源共享(CORS)問題是一個(gè)常見的挑戰(zhàn),接下來,我將分享我是如何一步步解決這個(gè)問題的,包括中間的一些試錯(cuò)過程,希望能夠幫助到正在經(jīng)歷類似問題的你2024-08-08

