福建城鄉(xiāng)建設(shè)部網(wǎng)站首頁(yè)競(jìng)價(jià)培訓(xùn)班
文章目錄
- 一、簡(jiǎn)單介紹
- 二、注解說(shuō)明
- 1. 注解源碼
- ① @PropertySource注解
- ② @PropertySources注解
- 2. 注解使用場(chǎng)景
- 3. 使用案例
- (1)新增test.properties文件
- (2)新增PropertySourceConfig類(lèi)
- (3)新增PropertySourceTest類(lèi)
- (4)運(yùn)行PropertySourceTest類(lèi)
一、簡(jiǎn)單介紹
在日常開(kāi)發(fā)中,你有沒(méi)有遇到過(guò)這樣一種場(chǎng)景:項(xiàng)目中需要編寫(xiě)很多配置文件,將一些系統(tǒng)信息配置化,此時(shí),往往需要編寫(xiě)專(zhuān)門(mén)的工具類(lèi)或者方法來(lái)讀取并解析這些配置文件,將配置文件中的配置項(xiàng)內(nèi)容加載到系統(tǒng)內(nèi)存中。后續(xù)在使用這些配置項(xiàng)時(shí),可以直接通過(guò)工具類(lèi)或者方法獲取加載到內(nèi)存中的配置項(xiàng)。
@PropertySource注解就是Spring中提供的一個(gè)可以加載配置文件的注解,并且可以將配置文件中的內(nèi)容存放到Spring的環(huán)境變量中。
二、注解說(shuō)明
@PropertySource注解是Spring中提供的一個(gè)通過(guò)指定配置文件位置來(lái)加載配置文件的注解,并且可以將配置文件中的內(nèi)容存放到Spring的環(huán)境變量中。除了可以通過(guò)Spring的環(huán)境變量讀取配置項(xiàng)之外,還可以通過(guò)@Value注解獲取配置項(xiàng)的值。
另外,Spring中還提供了一個(gè)@PropertySources注解,在@PropertySources注解中,可以引入多個(gè)@PropertySource注解。
1. 注解源碼
Spring中提供了@PropertySource和@PropertySources兩個(gè)注解來(lái)加載配置文件。
① @PropertySource注解
@PropertySource注解只能標(biāo)注到類(lèi)上,能夠通過(guò)指定配置文件的位置來(lái)加載配置文件,@PropertySource注解除了可以加載properties配置文件外,也可以加載xml配置文件和yml配置文件。如果加載yml配置文件時(shí),可以自定義PropertySourceFactory實(shí)現(xiàn)yml配置文件的解析操作。
@PropertySource注解的源碼詳見(jiàn):
/*** @author Chris Beams* @author Juergen Hoeller* @author Phillip Webb* @author Sam Brannen* @since 3.1*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {String name() default "";String[] value();/*** @since 4.0*/boolean ignoreResourceNotFound() default false;/*** @since 4.3*/String encoding() default "";/*** @since 4.3*/Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}
從源碼可以看出,@PropertySource注解是從Spring3.1版本開(kāi)始提供的注解,注解中各個(gè)屬性的含義如下所示:
- name:表示加載的資源的名稱(chēng),如果為空,則會(huì)根據(jù)加載的配置文件自動(dòng)生成一個(gè)名稱(chēng)。
- value:表示加載的資源的路徑,這個(gè)路徑可以是類(lèi)路徑,也可以是文件路徑。
- ignoreResourceNotFound:表示當(dāng)配置文件未找到時(shí),是否忽略文件未找到的錯(cuò)誤。默認(rèn)值為false,也就是說(shuō)當(dāng)未找到配置文件時(shí),Spring啟動(dòng)就會(huì)報(bào)錯(cuò)。
- encoding:表示解析配置文件使用的字符集編碼。
factory:表示讀取對(duì)應(yīng)配置文件的工廠類(lèi),默認(rèn)的工廠類(lèi)是PropertySourceFactory。
② @PropertySources注解
除了@PropertySource注解,Spring中還提供了一個(gè)@PropertySources注解。
/*** @author Phillip Webb* @since 4.0*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PropertySources {PropertySource[] value();
}
從源碼可以看出,@PropertySources是從Spring4.0版本開(kāi)始提供的注解,在@PropertySources注解中,只提供了一個(gè)PropertySource數(shù)組類(lèi)型的value屬性。所以,@PropertySources注解可以引入多個(gè)@PropertySource注解。
2. 注解使用場(chǎng)景
- 在基于Spring的注解開(kāi)發(fā)項(xiàng)目的過(guò)程中,由于不再使用Spring的XML文件進(jìn)行配置,如果將配置項(xiàng)直接寫(xiě)到類(lèi)中,就會(huì)造成配置項(xiàng)與類(lèi)的緊耦合,后續(xù)對(duì)于配置項(xiàng)的修改操作非常不方便,不利于項(xiàng)目的維護(hù)和擴(kuò)展。此時(shí),可以將這些配置項(xiàng)寫(xiě)到properties文件或者yml文件中,通過(guò)@PropertySource注解加載配置文件。
- 另外,如果項(xiàng)目本身就存在大量的properties配置文件或者yml配置文件,也可以統(tǒng)一由Spring的@PropertySource注解進(jìn)行加載。
3. 使用案例
本節(jié),主要實(shí)現(xiàn)一個(gè)通過(guò)@PropertySource注解加載properties配置文件,將properties配置文件中的配置項(xiàng)加載到Spring的環(huán)境變量中,獲取Spring環(huán)境變量中配置項(xiàng)的值,并進(jìn)行打印。案例的具體實(shí)現(xiàn)步驟如下所示。
(1)新增test.properties文件
resources目錄下新增test.properties文件,文件內(nèi)容如下所示:
name=lwk
age=18
(2)新增PropertySourceConfig類(lèi)
@Configuration
@PropertySource(value = "classpath:test.properties")
public class PropertySourceConfig {
}
可以看到,PropertySourceConfig類(lèi)是Spring的配置類(lèi),并且使用@PropertySource注解指定了test.properties配置文件的路徑。
(3)新增PropertySourceTest類(lèi)
public class PropertySourceTest {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PropertySourceConfig.class);ConfigurableEnvironment environment = context.getEnvironment();System.out.println(environment.getProperty("name") + " ====>>> " + environment.getProperty("age"));}
}
可以看到,在PropertySourceTest類(lèi)的main()方法中,通過(guò)AnnotationConfigApplicationContext類(lèi)的對(duì)象獲取到ConfigurableEnvironment類(lèi)型的環(huán)境變量對(duì)象environment,然后通過(guò)environment對(duì)象獲取配置文件中的name和age的值并進(jìn)行打印。
(4)運(yùn)行PropertySourceTest類(lèi)
可以看到,正確的輸出了配置文件中的值。
lwk ====>>> 18
說(shuō)明:使用@PropertySource注解可以加載properties配置文件中的配置項(xiàng),并將配置項(xiàng)加載到Spring的環(huán)境變量中,通過(guò)Spring的環(huán)境變量就可以獲取到配置項(xiàng)的值。