中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

福建城鄉(xiāng)建設(shè)部網(wǎng)站首頁(yè)競(jìng)價(jià)培訓(xùn)班

福建城鄉(xiāng)建設(shè)部網(wǎng)站首頁(yè),競(jìng)價(jià)培訓(xùn)班,怎么研發(fā)軟件app,java鮮花商城網(wǎng)站設(shè)計(jì)文章目錄一、簡(jiǎn)單介紹二、注解說(shuō)明1. 注解源碼① PropertySource注解② PropertySources注解2. 注解使用場(chǎng)景3. 使用案例(1)新增test.properties文件(2)新增PropertySourceConfig類(lèi)(3)新增PropertySourceTe…

文章目錄

  • 一、簡(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)的值。

http://www.risenshineclean.com/news/2474.html

相關(guān)文章:

  • 紹興公司企業(yè)名單武漢seo優(yōu)化代理
  • 做汽車(chē)行業(yè)必須注冊(cè)際零件網(wǎng)站必應(yīng)搜索國(guó)際版
  • 邢臺(tái)商城類(lèi)網(wǎng)站建設(shè)企業(yè)qq郵箱
  • 百度指數(shù)的網(wǎng)站谷歌搜索入口365
  • 網(wǎng)站制作的公司哪個(gè)好南寧seo營(yíng)銷(xiāo)推廣
  • 長(zhǎng)沙哪個(gè)網(wǎng)站建設(shè)最好重慶可靠的關(guān)鍵詞優(yōu)化研發(fā)
  • 網(wǎng)站開(kāi)發(fā)常用的谷歌插件女教師遭網(wǎng)課入侵視頻大全播放
  • 高密市政府建設(shè)局網(wǎng)站臺(tái)州網(wǎng)站制作維護(hù)
  • 汕頭市門(mén)戶(hù)網(wǎng)站建設(shè)屬性詞 關(guān)鍵詞 核心詞
  • 深圳軟件有限公司企業(yè)網(wǎng)站優(yōu)化關(guān)鍵詞
  • 電商設(shè)計(jì)網(wǎng)站模板搜索引擎優(yōu)化seo公司
  • 做很多網(wǎng)站省委副書(shū)記
  • 頂呱呱網(wǎng)站做的怎么樣?xùn)|莞網(wǎng)站推廣大全
  • 深圳專(zhuān)業(yè)手機(jī)網(wǎng)站建設(shè)重慶seo按天收費(fèi)
  • 河北涿州網(wǎng)站建設(shè)黑科技推廣軟件
  • 前端做網(wǎng)站一般用什么框架中國(guó)十大搜索引擎網(wǎng)站
  • ps做旅游網(wǎng)站整合營(yíng)銷(xiāo)方案
  • 公司網(wǎng)站怎么做能被別人搜索到個(gè)人網(wǎng)頁(yè)
  • 做微網(wǎng)站的第三方登錄界面百度推廣費(fèi)用可以退嗎
  • 網(wǎng)站ui 特點(diǎn)建立免費(fèi)網(wǎng)站
  • 保定網(wǎng)站seoseo外包優(yōu)化公司
  • 平面設(shè)計(jì)接單appseo內(nèi)部?jī)?yōu)化包括哪些內(nèi)容
  • 做暖暖無(wú)碼網(wǎng)站國(guó)通快速建站
  • 寶安中心做網(wǎng)站網(wǎng)絡(luò)推廣seo教程
  • 企業(yè)網(wǎng)站開(kāi)發(fā) metinfo網(wǎng)站搜索引擎優(yōu)化診斷
  • 備案 手機(jī)網(wǎng)站銷(xiāo)售管理怎么帶團(tuán)隊(duì)
  • 谷歌做公司網(wǎng)站需要多少錢(qián)google關(guān)鍵詞推廣
  • 網(wǎng)站會(huì)員體系百度權(quán)重
  • 長(zhǎng)春網(wǎng)站開(kāi)發(fā)senluowx360搜索推廣官網(wǎng)
  • 渝北網(wǎng)站建設(shè)鄭州seo優(yōu)化外包公司