關(guān)于@PropertySource配置的用法解析
@PropertySource配置用法
功能
加載指定的屬性文件(*.properties)到 Spring 的 Environment 中。可以配合 @Value 和@ConfigurationProperties 使用。
@PropertySource 和 @Value組合使用,可以將自定義屬性文件中的屬性變量值注入到當(dāng)前類的使用@Value注解的成員變量中。
@PropertySource 和 @ConfigurationProperties組合使用,可以將屬性文件與一個(gè)Java類綁定,將屬性文件中的變量值注入到該Java類的成員變量中。
源碼
package org.springframework.context.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.core.io.support.PropertySourceFactory; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(PropertySources.class) public @interface PropertySource { /** * 屬性源的名稱 */ String name() default ""; /** * 屬性文件的存放路徑 */ String[] value(); /** * 如果指定的屬性源不存在,是否要忽略這個(gè)錯(cuò)誤 */ boolean ignoreResourceNotFound() default false; /** * 屬性源的編碼格式 */ String encoding() default ""; /** * 屬性源工廠 */ Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class; }
使用示例
屬性文件:demo.properties
demo.name=huang demo.sex=1 demo.type=demo
示例一:@PropertySource + @Value
package com.huang.pims.demo.props; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource(value = {"demo/props/demo.properties"}) public class ReadByPropertySourceAndValue { ? ? @Value("${demo.name}") ? ? private String name; ? ? @Value("${demo.sex}") ? ? private int sex; ? ? @Value("${demo.type}") ? ? private String type; ? ? @Override ? ? public String toString() { ? ? ? ? return "ReadByPropertySourceAndValue{" + ? ? ? ? ? ? ? ? "name='" + name + '\'' + ? ? ? ? ? ? ? ? ", sex=" + sex + ? ? ? ? ? ? ? ? ", type='" + type + '\'' + ? ? ? ? ? ? ? ? '}'; ? ? } }
示例二:@PropertySource 和 @ConfigurationProperties
package com.huang.pims.demo.props; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource(value = {"demo/props/demo.properties"}) @ConfigurationProperties(prefix = "demo") public class ReadByPropertySourceAndConfProperties { ? ? private String name; ? ? private int sex; ? ? private String type; ? ? public void setName(String name) { ? ? ? ? this.name = name; ? ? } ? ? public void setSex(int sex) { ? ? ? ? this.sex = sex; ? ? } ? ? public void setType(String type) { ? ? ? ? this.type = type; ? ? } ? ? public String getName() { ? ? ? ? return name; ? ? } ? ? public int getSex() { ? ? ? ? return sex; ? ? } ? ? public String getType() { ? ? ? ? return type; ? ? } ? ? @Override ? ? public String toString() { ? ? ? ? return "ReadByPropertySourceAndConfProperties{" + ? ? ? ? ? ? ? ? "name='" + name + '\'' + ? ? ? ? ? ? ? ? ", sex=" + sex + ? ? ? ? ? ? ? ? ", type='" + type + '\'' + ? ? ? ? ? ? ? ? '}'; ? ? } }
示例測(cè)試
package com.huang.pims.demo.runners; import com.huang.pims.demo.props.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class OutputPropsRunner implements CommandLineRunner { ? ? private static final Logger LOGGER = LoggerFactory.getLogger(OutputPropsRunner.class); ? ? @Autowired ? ? private ReadByPropertySourceAndValue readByPropertySourceAndValue; ? ? @Autowired ? ? private ReadByPropertySourceAndConfProperties readByPropertySourceAndConfProperties; ? ? @Override ? ? public void run(String... args) throws Exception { ? ? ? ? LOGGER.info(readByPropertySourceAndValue.toString()); ? ? ? ? LOGGER.info(readByPropertySourceAndConfProperties.toString()); ? ? } }
啟動(dòng)項(xiàng)目即可看到效果。
從截圖中可以看出,需要讀取的屬性配置,都已經(jīng)成功讀取出來了。
@PropertySource注解
@PropertySource是Spring boot為了方便引入properties配置文件提供的一個(gè)注解,可以標(biāo)注在SpringBoot的啟動(dòng)類上,還可以標(biāo)注在配置類(使用@Configuration標(biāo)注的類)上。
例如
@PropertySource(value = {"classpath:box.properties"})
將classpath下的box.properties,注入到Spring環(huán)境中,使用@Value("${key}")取值。
示例
box.properties文件:
# 工具箱配置? preserveFilePath=/box/webserver/uploadfile/preservefile/
注入:
@SpringBootApplication @PropertySource(value = {"classpath:box.properties"}) public class ToolboxApiApplication { ? ? ? public static void main(String[] args) { ? ? ? ? SpringApplication.run(ToolboxApiApplication.class, args); ? ? }? }
取值:
@RestController public class DeleteFileController { ? ? @Value("${preserveFilePath}") ? ? private String preserveFilePath; ? ? ? @GetMapping("/deleteFile") ? ? public void test(){ ? ? ? ? System.out.println(preserveFilePath); ? ? } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Eclipse將Maven項(xiàng)目打成jar包的方法
這篇文章主要介紹了Eclipse將Maven項(xiàng)目打成jar包的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2007-09-09Java并發(fā)系列之ConcurrentHashMap源碼分析
這篇文章主要為大家詳細(xì)分析了Java并發(fā)系列之ConcurrentHashMap源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Mybatis中實(shí)體類屬性與數(shù)據(jù)列表間映射方法介紹
這篇文章主要介紹了Mybatis中實(shí)體類屬性與數(shù)據(jù)列表間映射方法介紹,一共四中方法,供大家參考。2017-10-10Spring security如何實(shí)現(xiàn)記錄用戶登錄時(shí)間功能
這篇文章主要介紹了Spring security如何實(shí)現(xiàn)記錄用戶登錄時(shí)間功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Spring mvc JSON數(shù)據(jù)交換格式原理解析
這篇文章主要介紹了Spring mvc JSON數(shù)據(jù)交換格式原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03關(guān)于Spring中一級(jí)緩存、二級(jí)緩存和三級(jí)緩存的那些事
Spring解決循環(huán)依賴的核心思想在于提前曝,下面這篇文章主要給大家介紹了關(guān)于Spring中一級(jí)緩存、二級(jí)緩存和三級(jí)緩存的那些事,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02java實(shí)現(xiàn)將字符串中首字母轉(zhuǎn)換成大寫,其它全部轉(zhuǎn)換成小寫的方法示例
這篇文章主要介紹了java實(shí)現(xiàn)將字符串中首字母轉(zhuǎn)換成大寫,其它全部轉(zhuǎn)換成小寫的方法,涉及java字符串遍歷、轉(zhuǎn)換、拼接等相關(guān)操作技巧,需要的朋友可以參考下2019-06-06