最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot讀取自定義配置文件

 更新時(shí)間:2021年05月28日 16:05:06   作者:碼農(nóng)小胖哥  
在Spring Boot項(xiàng)目中我們經(jīng)常需要讀取application.yml配置文件的自定義配置,今天就來(lái)羅列一下從yaml讀取配置文件的一些常用手段和方法。

@Value

首先,會(huì)想到使用@Value注解,該注解只能去解析yaml文件中的簡(jiǎn)單類型,并綁定到對(duì)象屬性中去。

felord:
  phone: 182******32
  def:
    name: 碼農(nóng)小胖哥
    blog: felord.cn
    we-chat: MSW_623
  dev:
    name: 碼農(nóng)小胖哥
    blog: felord.cn
    we-chat: MSW_623
  type: JUEJIN

對(duì)于上面的yaml配置,如果我們使用@Value注解的話,冒號(hào)后面直接有值的key才能正確注入對(duì)應(yīng)的值。例如felord.phone我們可以通過(guò)@Value獲取,但是felord.def不行,因?yàn)閒elord.def后面沒(méi)有直接的值,它還有下一級(jí)選項(xiàng)。另外@Value不支持yaml松散綁定語(yǔ)法,也就是說(shuō)felord.def.weChat獲取不到felord.def.we-chat的值。

@Value是通過(guò)使用Spring的SpEL表達(dá)式來(lái)獲取對(duì)應(yīng)的值的:

// 獲取 yaml 中 felord.phone的值 并提供默認(rèn)值 UNKNOWN
@Value("${felord.phone:UNKNOWN}")
 private String phone;

@Value的使用場(chǎng)景是只需要獲取配置文件中的某項(xiàng)值的情況下,如果我們需要將一個(gè)系列的值進(jìn)行綁定注入就建議使用復(fù)雜對(duì)象的形式進(jìn)行注入了。

@ConfigurationProperties

@ConfigurationProperties注解提供了我們將多個(gè)配置選項(xiàng)注入復(fù)雜對(duì)象的能力。它要求我們指定配置的共同前綴。比如我們要綁定felord.def下的所有配置項(xiàng):

package cn.felord.yaml.properties;
 
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
 
import static cn.felord.yaml.properties.FelordDefProperties.PREFIX;
 
/**
 * @author felord.cn
 */
@Data
@ConfigurationProperties(PREFIX)
public class FelordDefProperties {
    static final String PREFIX = "felord.def";
    private String name;
    private String blog;
    private String weChat;
}

我們注意到我們可以使用weChat接收we-chat的值,因?yàn)檫@種形式支持從駝峰camel-case到短橫分隔命名kebab-case的自動(dòng)轉(zhuǎn)換。

如果我們使用@ConfigurationProperties的話建議配置類命名后綴為Properties,比如Redis的后綴就是RedisProperties,RabbitMQ的為RabbitProperties。

另外我們?nèi)绻脒M(jìn)行嵌套的話可以借助于@NestedConfigurationProperty注解實(shí)現(xiàn)。也可以借助于內(nèi)部類。這里用內(nèi)部類實(shí)現(xiàn)將開(kāi)頭yaml中所有的屬性進(jìn)行注入:

package cn.felord.yaml.properties;
 
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
 
import static cn.felord.yaml.properties.FelordProperties.PREFIX;
 
 
/**
 * 內(nèi)部類和枚舉配置.
 *
 * @author felord.cn
 */
@Data
@ConfigurationProperties(PREFIX)
public class FelordProperties {
 
    static final String PREFIX = "felord";
    private Def def;
    private Dev dev;
    private Type type;
 
    @Data
    public static class Def {
        private String name;
        private String blog;
        private String weChat;
    }
 
    @Data
    public static class Dev {
        private String name;
        private String blog;
        private String weChat;
    }
 
    public enum Type {
        JUEJIN,
        SF,
        OSC,
        CSDN
    }
}

單獨(dú)使用@ConfigurationProperties的話依然無(wú)法直接使用配置對(duì)象FelordDefProperties,因?yàn)樗](méi)有被注冊(cè)為Spring Bean。我們可以通過(guò)兩種方式來(lái)使得它生效。

顯式注入 Spring IoC

你可以使用@Component、@Configuration等注解將FelordDefProperties注入Spring IoC使之生效。

package cn.felord.yaml.properties;
 
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
import static cn.felord.yaml.properties.FelordDefProperties.PREFIX;
 
/**
 * 顯式注入Spring IoC
 * @author felord.cn
 */
@Data
@Component
@ConfigurationProperties(PREFIX)
public class FelordDefProperties {
    static final String PREFIX = "felord.def";
    private String name;
    private String blog;
    private String weChat;
}

@EnableConfigurationProperties

我們還可以使用注解@EnableConfigurationProperties進(jìn)行注冊(cè),這樣就不需要顯式聲明配置類為Spring Bean了。

package cn.felord.yaml.configuration;
 
import cn.felord.yaml.properties.FelordDevProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
 
/**
 * 使用 {@link EnableConfigurationProperties} 注冊(cè) {@link FelordDevProperties}使之生效
 * @author felord.cn
 */
@EnableConfigurationProperties({FelordDevProperties.class})
@Configuration
public class FelordConfiguration {
}

該注解需要顯式的注冊(cè)對(duì)應(yīng)的配置類。

@ConfigurationPropertiesScan

在Spring Boot 2.2.0.RELEASE中提供了一個(gè)掃描注解@ConfigurationPropertiesScan。它可以掃描特定包下所有的被@ConfigurationProperties標(biāo)記的配置類,并將它們進(jìn)行IoC注入。

package cn.felord.yaml;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
 
/**
 * {@link ConfigurationPropertiesScan} 同 {@link EnableConfigurationProperties} 二選一
 *
 * @see cn.felord.yaml.configuration.FelordConfiguration
 * @author felord.cn
 */
@ConfigurationPropertiesScan
@SpringBootApplication
public class SpringBootYamlApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringBootYamlApplication.class, args);
    }
 
}

這非常適合自動(dòng)注入和批量注入配置類的場(chǎng)景,但是有版本限制,必須在2.2.0及以上。

@PropertySource注解

@PropertySource可以用來(lái)加載指定的配置文件,默認(rèn)它只能加載*.properties文件,不能加載諸如yaml等文件。

@PropertySource相關(guān)屬性介紹

  • value:指明加載配置文件的路徑。
  • ignoreResourceNotFound:指定的配置文件不存在是否報(bào)錯(cuò),默認(rèn)是false。當(dāng)設(shè)置為 true 時(shí),若該文件不存在,程序不會(huì)報(bào)錯(cuò)。實(shí)際項(xiàng)目開(kāi)發(fā)中,最好設(shè)置 ignoreResourceNotFound 為 false。
  • encoding:指定讀取屬性文件所使用的編碼,我們通常使用的是UTF-8。
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Configuration
@PropertySource(value = {"classpath:common.properties"},ignoreResourceNotFound=false,encoding="UTF-8")
@ConfigurationProperties(prefix = "author")
public class Author {
    private String name;
    private String job;
    private String sex;
}

有小伙伴也許發(fā)現(xiàn)示例上的@ConfigurationProperties注解了。當(dāng)我們使用@Value需要注入的值較多時(shí),代碼就會(huì)顯得冗余。我們可以使用@ConfigurationProperties 中的 prefix 用來(lái)指明我們配置文件中需要注入信息的前綴

前邊提到了用@PropertySource只能加載*.properties文件,但如果我們項(xiàng)目的配置文件不是*.properties這種類型,而是其他類型,諸如yaml,此時(shí)我們可以通過(guò)實(shí)現(xiàn)PropertySourceFactory接口,重寫(xiě)createPropertySource方法,就能實(shí)現(xiàn)用@PropertySource也能加載yaml等類型文件。

public class YamlPropertySourceFactory implements PropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String sourceName, EncodedResource resource) throws IOException {
        Properties propertiesFromYaml = loadYaml(resource);
        if(StringUtils.isBlank(sourceName)){
            sourceName =  resource.getResource().getFilename();;
        }

        return new PropertiesPropertySource(sourceName, propertiesFromYaml);
    }
    private Properties loadYaml(EncodedResource resource) throws FileNotFoundException {
        try {
            YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
            factory.setResources(resource.getResource());
            factory.afterPropertiesSet();
            return factory.getObject();
        } catch (IllegalStateException e) {
            // for ignoreResourceNotFound
            Throwable cause = e.getCause();
            if (cause instanceof FileNotFoundException)
                throw (FileNotFoundException) e.getCause();
            throw e;
        }
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Configuration
@PropertySource(factory = YamlPropertySourceFactory.class,value = {"classpath:user.yml"},ignoreResourceNotFound=false,encoding="UTF-8")
@ConfigurationProperties(prefix = "user")
public class User {
    private String username;

    private String password;
}

使用EnvironmentPostProcessor加載自定義配置文件

1、實(shí)現(xiàn)EnvironmentPostProcessor接口,重寫(xiě)postProcessEnvironment方法

@Slf4j
public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {

        Properties properties = new Properties();

        try {
            properties.load(new InputStreamReader(CustomEnvironmentPostProcessor.class.getClassLoader().getResourceAsStream("custom.properties"),"UTF-8"));

            PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("custom",properties);
            environment.getPropertySources().addLast(propertiesPropertySource);
        } catch (IOException e) {
          log.error(e.getMessage(),e);
        }

    }
}

2、在META-INF下創(chuàng)建spring.factories

spring.factories文件內(nèi)容如下:
org.springframework.boot.env.EnvironmentPostProcessor=com.github.lybgeek.env.CustomEnvironmentPostProcessor

1、2步驟實(shí)現(xiàn)完后,就可以在代碼中直接用@Value的方式獲取自定義配置文件內(nèi)容了

讀取的自定義配置文件內(nèi)容的實(shí)現(xiàn)方法有多種多樣,除了上面的方法,還可以在以-jar方式啟動(dòng)時(shí),執(zhí)行形如下命令

java -jar project.jar --spring.config.location=classpath:/config/custom.yml

也能實(shí)現(xiàn)。還可以干脆自定義配置文件都以application-*為前綴,比如application-custom,然后在application.properties,使用spring.profiles.include=custom或者spring.profiles.active=custom也可以實(shí)現(xiàn)

總結(jié)

日常開(kāi)發(fā)中單個(gè)屬性推薦使用@Value,如果同一組屬性為多個(gè)則推薦@ConfigurationProperties。需要補(bǔ)充一點(diǎn)的是@ConfigurationProperties還支持使用 JSR303 進(jìn)行屬性校驗(yàn)。好了今天的教程就到這里

相關(guān)的demo地址

https://gitee.com/felord/spring-boot-yml.git

https://github.com/lyb-geek/springboot-learning/tree/master/springboot-outside-config

以上就是Spring Boot讀取自定義配置文件的詳細(xì)內(nèi)容,更多關(guān)于Spring Boot讀取自定義配置文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • @RequestAttribute和@RequestParam注解的區(qū)別及說(shuō)明

    @RequestAttribute和@RequestParam注解的區(qū)別及說(shuō)明

    這篇文章主要介紹了@RequestAttribute和@RequestParam注解的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java8 接口默認(rèn)方法和靜態(tài)方法

    Java8 接口默認(rèn)方法和靜態(tài)方法

    這篇文章主要介紹了Java8 接口默認(rèn)方法和靜態(tài)方法,在默認(rèn)接口中使用關(guān)鍵字default聲明并提供具體實(shí)現(xiàn),而且該方法不需要添加public關(guān)鍵字就可以公開(kāi)調(diào)用,甚至你可以在其實(shí)現(xiàn)類中覆寫(xiě),帶著對(duì)默認(rèn)接口的方法和小編一起探索下面文章內(nèi)容的靜態(tài)方法吧
    2021-10-10
  • Java開(kāi)啟JMX遠(yuǎn)程監(jiān)控服務(wù)配置

    Java開(kāi)啟JMX遠(yuǎn)程監(jiān)控服務(wù)配置

    這篇文章主要為大家介紹了Java開(kāi)啟JMX遠(yuǎn)程監(jiān)控的服務(wù)配置,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • java設(shè)計(jì)模式之工廠方法詳解

    java設(shè)計(jì)模式之工廠方法詳解

    這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之工廠方法的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Java?synchronized關(guān)鍵字性能考量及優(yōu)化探索

    Java?synchronized關(guān)鍵字性能考量及優(yōu)化探索

    這篇文章主要為大家介紹了Java?synchronized關(guān)鍵字性能考量及優(yōu)化探索示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Spring Boot Filter 過(guò)濾器的使用方式

    Spring Boot Filter 過(guò)濾器的使用方式

    這篇文章主要介紹了Spring Boot Filter 過(guò)濾器的使用方式,文章通過(guò)圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • 使用@CacheEvict清除指定下所有緩存

    使用@CacheEvict清除指定下所有緩存

    這篇文章主要介紹了使用@CacheEvict清除指定下所有緩存,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • kafka監(jiān)聽(tīng)問(wèn)題的解決和剖析

    kafka監(jiān)聽(tīng)問(wèn)題的解決和剖析

    這篇文章主要給大家介紹了關(guān)于kafka監(jiān)聽(tīng)問(wèn)題的解決和剖析的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Spring Boot 控制層之參數(shù)傳遞方法詳解

    Spring Boot 控制層之參數(shù)傳遞方法詳解

    這篇文章主要介紹了Spring Boot 控制層之參數(shù)傳遞方法詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • RabbitMq中channel接口的幾種常用參數(shù)詳解

    RabbitMq中channel接口的幾種常用參數(shù)詳解

    這篇文章主要介紹了RabbitMq中channel接口的幾種常用參數(shù)詳解,RabbitMQ 不會(huì)為未確認(rèn)的消息設(shè)置過(guò)期時(shí)間,它判斷此消息是否需要重新投遞給消費(fèi)者的唯一依據(jù)是消費(fèi)該消息的消費(fèi)者連接是否己經(jīng)斷開(kāi),需要的朋友可以參考下
    2023-08-08

最新評(píng)論

崇阳县| 蒲城县| 甘肃省| 泗洪县| 呼图壁县| 阆中市| 吉安市| 宣威市| 平南县| 石屏县| 长垣县| 广昌县| 资中县| 马公市| 军事| 海丰县| 开化县| 三亚市| 淮安市| 寻乌县| 崇明县| 平度市| 黄梅县| 鲜城| 偏关县| 民和| 上饶市| 比如县| 富宁县| 蒲江县| 洮南市| 普定县| 桓仁| 秦皇岛市| 大渡口区| 景德镇市| 舟曲县| 朝阳县| 乌兰县| 南京市| 日土县|