日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

@PropertySource与@ConfigurationProperties多种方式读取配置文件详解,附带@PropertySources使用说明

發(fā)布時間:2025/1/21 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 @PropertySource与@ConfigurationProperties多种方式读取配置文件详解,附带@PropertySources使用说明 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

兩個注解都可以讀取properties文件或者xml文件,二者可以單獨使用也可以結(jié)合使用。

一、 @PropertySource結(jié)合@Value讀取指定配置文件

1、準(zhǔn)備一個properties文件放在項目下

demo.name=zhangsan demo.age=25 demo.address=hangzhou

2、使用@PropertySource讀取數(shù)據(jù)

import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;@Data @Component @PropertySource("classpath:demo.properties") public class ReadConfByValue {@Value("${demo.name}")private String name;@Value("${demo.age}")private String age;@Value("${demo.address}")private String address;}

3、查看效果
讓讀取到的數(shù)據(jù)在springboot啟動時控制臺顯示

import com.gao.readconf.ReadConfByValue; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component;@Component @Log4j2 public class InitRunner implements CommandLineRunner {@Autowiredprivate ReadConfByValue conf;@Overridepublic void run(String... args) throws Exception {log.info(conf.toString());} }

4、驗證yml文件能否讀取
準(zhǔn)備一個demo.yml文件

demo:name: lisiage: 130address: shenzhen

將@PropertySource(“classpath:demo.yml”)改為,運行報錯。源碼給了value的說明,指明@PropertySource僅支持讀取properties文件與xml文件。

二、 @PropertySource結(jié)合Environment讀取配置文件

import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component;@Component @Log4j2 @PropertySource("classpath:demo.properties") public class ReadConfByEnvironment {@Autowiredpublic Environment environment;@Beanpublic void toReadEnvConf(){log.error("**********通過Env獲取數(shù)據(jù)*********");log.error(environment.getProperty("demo.name"));log.error(environment.getProperty("demo.age"));log.error(environment.getProperty("demo.address"));log.error("**********End*********");} }

三、 @PropertySource讀取多個properties文件

@Data @Component @PropertySource(value = {"classpath:demo.properties","classpath:demoadd.properties"}) public class Read2ConfByValue {@Value("${demo.name}")private String name;@Value("${demo.age}")private String age;@Value("${demo.address}")private String address;@Value("${demo1.birth}")private String birth;@Value("${demo1.phone}")private String phone;@Value("${demo1.hobby}")private String hobby; }

修改對應(yīng)的InitRunner并運行項目

四、@PropertySource 結(jié)合 @ConfigurationProperties讀取配置文件

1、準(zhǔn)備另一個properties文件demoadd.properties:

demo1.birth=2020.9.2 demo1.phone=13020122365 demo1.hobby=codingdemo2.name=lisi demo2.age=120 demo2.address=beijing

2、@PropertySource(“classpath:demoadd.properties”)作用是指定文件,@ConfigurationProperties(prefix=“demo1”)限定前綴為demo1,前綴為demo2的字段讀取不到。兩者結(jié)合使用時相當(dāng)于讀取到@PropertySource指定的文件后,再通過@ConfigurationProperties進行數(shù)據(jù)過濾,僅獲取指定前綴的數(shù)據(jù)。

@Data @Component @PropertySource("classpath:demoadd.properties") @ConfigurationProperties(prefix="demo1") public class ReadConfByConfigurationProperties {private String birth;private String phone;private String hobby;}

3、修改InitRunner,啟動項目

@Component @Log4j2 public class InitRunner implements CommandLineRunner {@Autowiredprivate ReadConfByValue conf;@Autowiredprivate ReadConfByConfigurationProperties readconf;@Overridepublic void run(String... args) throws Exception {log.info(conf.toString());log.info(readconf.toString());} }

4、如何讀取多前綴數(shù)據(jù)?

從源碼中看出,prefix并不是數(shù)組形式,注釋中也說明了A valid prefix,表明不支持多前綴。

五、 @ConfigurationProperties單獨使用

1、不通過@PropertySource指定固定文件,僅指定前綴,仍能讀取到demoadd.properties中的數(shù)據(jù)

@Data @Component //@PropertySource("classpath:demoadd.properties") @ConfigurationProperties(prefix="demo1") public class ReadConfByConfigurationProperties {private String birth;private String phone;private String hobby; }

2、那如果指定的prefix重復(fù)呢?
修改demo.properties文件,添加demo1屬性,執(zhí)行項目

demo.name=zhangsan demo.age=25 demo.address=hangzhoudemo1.birth=1000.8.12 demo1.phone=18812568532 demo1.hobby=playing

本以為會報錯,,誰知結(jié)果沒變,還是正常輸出

這不會是所有properties文件都讀一遍,保留最后/或者第一次讀取的文件中數(shù)據(jù)吧,
把demo.properties名字改了個遍結(jié)果也還是一動不動。。。(此處疑問保留)

3、@ConfigurationProperties能不能讀取yml文件
試了試讀取demo.yml,讀取失敗,字段全為null
如果是application.yml呢,在application.yml中添加:

demo:name: lisiage: 180address: shenzhen


執(zhí)行項目

看到結(jié)果呵呵。。能讀到,并且其他文件demo.properties、demoadd.properties里的demo前綴字段數(shù)據(jù)全給替換了

六、@PropertySources

實現(xiàn)多個配置文件的讀取,可是@PropertySource本來就有這個功能啊,為什么還要搞個這?
源碼:

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface PropertySources {PropertySource[] value();}

使用方法:

@Data @Component @PropertySources(value = {@PropertySource("classpath:demo.properties"),@PropertySource("classpath:demoadd.properties")}) public class ReadConfByValue {@Value("${demo.name}")private String name;@Value("${demo.age}")private String age;@Value("${demo.address}")private String address;}

總結(jié)

以上是生活随笔為你收集整理的@PropertySource与@ConfigurationProperties多种方式读取配置文件详解,附带@PropertySources使用说明的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。