溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springboot怎么通過@Value,@ConfigurationProperties獲取配置

發(fā)布時間:2022-03-19 08:56:26 來源:億速云 閱讀:198 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“springboot怎么通過@Value,@ConfigurationProperties獲取配置”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“springboot怎么通過@Value,@ConfigurationProperties獲取配置”吧!

通過@Value,@ConfigurationProperties獲取配置

spring boot 獲取配置項值

使用版本是1.5.4

舉例一個線程池的配置:

在application.yml添加配置項及值

    # 線程池配置
    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加載配置文件,將配置項與bean及屬性關(guān)聯(lián),指定ignoreUnknownFields當(dāng)有屬性未匹配到值時會拋出異常,用prefix指定配置項的前綴。

@ConfigurationProperties還支持層級結(jié)構(gòu)、 布爾、集合等類型的值注入

說下@ConfigurationProperties和@Value區(qū)別

 @Configuration@Value
功能批量注入配置文件中的屬性一個個指定
松散綁定(松散語法)支持不支持
SPEL語法不支持支持
JSR303數(shù)據(jù)校驗支持不支持
復(fù)雜類型封裝支持不支持

配置文件yml還是properties他們都能獲取到值;

如果說, 只是在某個業(yè)務(wù)邏輯中需要獲取一項配置文件中的某項值, 使用@Value

如果說,專門編寫了一個javaBean 來和配置文件進(jìn)行映射,我們就直接使用@ConfigurationProperties;

配置文件注入值數(shù)據(jù)校驗

@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;

到此,相信大家對“springboot怎么通過@Value,@ConfigurationProperties獲取配置”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI