国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

Spring Properties的使用和配置方法

2024-07-13 10:15:58
字體:
供稿:網(wǎng)友

對(duì) Spring 里面的 Properties 不理解的開發(fā)者可能會(huì)覺得有點(diǎn)亂,主要是因?yàn)榕渲梅绞胶芏喾N,使用方式也很多種。

本文不是原理分析、源碼分析文章,只是希望可以幫助讀者更好地理解和使用 Spring Properties。

Properties 的使用

本文的讀者都是使用過 Spring 的,先來看看 Properties 是怎么使用的,Spring 中常用的有以下幾種使用方式:

1. 在 xml 配置文件中使用

即自動(dòng)替換 ${} 里面的值。

<bean id="xxx" class="com.javadoop.Xxx">   <property name="url" value="${javadoop.jdbc.url}" /></bean>

2. 通過 @Value 注入使用

@Value("${javadoop.jdbc.url}")private String url;

3. 通過 Environment 獲取

此法有需要注意的地方。并不是所有的配置方式都支持通過 Environment 接口來獲取屬性值,親測(cè)只有使用注解 @PropertySource 的時(shí)候可以用,否則會(huì)得到 null ,至于怎么配置,下面馬上就會(huì)說。

@Autowiredprivate Environment env;public String getUrl() {  return env.getProperty("javadoop.jdbc.url");}

如果是 Spring Boot 的 application.properties 注冊(cè)的,那也是可以的。

Properties 配置

前面我們說了怎么使用我們配置的 Properties,那么該怎么配置呢?Spring 提供了很多種配置方式。

1. 通過 xml 配置

下面這個(gè)是最常用的配置方式了,很多項(xiàng)目都是這么寫的:

<context:property-placeholder location="classpath:sys.properties" />

2. 通過 @PropertySource 配置

前面的通過 xml 配置非常常用,但是如果你也有一種要 消滅所有 xml 配置文件 的沖動(dòng)的話,你應(yīng)該使用以下方式:

@PropertySource("classpath:sys.properties")@Configurationpublic class JavaDoopConfig {}

注意一點(diǎn),@PropertySource 在這里必須搭配 @Configuration 來使用,具體不展開說了。

3. PropertyPlaceholderConfigurer

如果讀者見過這個(gè),也不必覺得奇怪,在 Spring 3.1 之前,經(jīng)常就是這么使用的:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  <property name="locations">    <list>      <value>classpath:sys.properties</value>    </list>  </property>  <property name="ignoreUnresolvablePlaceholders" value="true"/>   <!-- 這里可以配置一些屬性 --></bean>

當(dāng)然,我們也可以用相應(yīng)的 java configuration 的版本:

@Beanpublic PropertyPlaceholderConfigurer propertiess() {  PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();  Resource[] resources = new ClassPathResource[]{new ClassPathResource("sys.properties")};  ppc.setLocations(resources);  ppc.setIgnoreUnresolvablePlaceholders(true);  return ppc;}

4. PropertySourcesPlaceholderConfigurer

到了 Spring 3.1 的時(shí)候,引入了 PropertySourcesPlaceholderConfigurer ,這是一個(gè)新的類,注意看和之前的 PropertyPlaceholderConfigurer 在名字上多了一個(gè) Sources ,所屬的包也不一樣,它在 Spring-Context 包中。

在配置上倒是沒有什么區(qū)別:

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">  <property name="locations">    <list>      <value>classpath:sys.properties</value>    </list>  </property>  <property name="ignoreUnresolvablePlaceholders" value="true"/>  <!-- 這里可以配置一些屬性 --></bean>

也來一個(gè) java configuration 版本吧:

@Beanpublic PropertySourcesPlaceholderConfigurer properties() {  PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();  Resource[] resources = new ClassPathResource[]{new ClassPathResource("sys.properties")};  pspc.setLocations(resources);  pspc.setIgnoreUnresolvablePlaceholders(true);  return pspc;}

Spring Boot 相關(guān)

Spring Boot 真的是好東西,開箱即用的感覺實(shí)在是太好了。這里簡(jiǎn)單介紹下相關(guān)的內(nèi)容。

快速生成一個(gè) Spring Boot 項(xiàng)目: https://start.spring.io/

application.properties

我們每個(gè)項(xiàng)目都默認(rèn)有一個(gè) application.properties 文件,這個(gè)配置文件不需要像前面說的那樣進(jìn)行 注冊(cè) ,Spring Boot 會(huì)幫我們 自動(dòng)注冊(cè) 。

當(dāng)然,也許你想換個(gè)名字也是可以的,在啟動(dòng)的時(shí)候指定你的文件名字就可以了:

java -Dspring.config.location=classpath:sys.properties -jar app.jar

application-{env}.properties

為了給不同的環(huán)境指定不同的配置,我們會(huì)用到這個(gè)。

比如測(cè)試環(huán)境和生產(chǎn)環(huán)境的數(shù)據(jù)庫(kù)連接信息就不一樣。

所以,在 application.properties 的基礎(chǔ)上,我們還需要新建 application-dev.properties 和 application-prd.properties,用于配置環(huán)境相關(guān)的信息,然后啟動(dòng)的時(shí)候指定環(huán)境。

java -Dspring.profiles.active=prd -jar app.jar

結(jié)果就是,application.properties 和 application-prd.properties 兩個(gè)文件中的配置都會(huì)注冊(cè)進(jìn)去,如果有重復(fù)的 key,application-prd.properties 文件中的優(yōu)先級(jí)較高。

@ConfigurationProperties

這個(gè)注解是 Spring Boot 中才有的。

即使大家不使用這個(gè)注解,大家也可能會(huì)在開源項(xiàng)目中看到這個(gè),這里簡(jiǎn)單介紹下。

來一個(gè)例子直觀一些。按照之前說的,在配置文件中填入下面的信息,你可以選擇寫入 application.properties 也可以用第一節(jié)介紹的方法。

javadoop.database.url=jdbc:mysql:javadoop.database.username=adminjavadoop.database.password=admin123456

java 文件:

@Configuration@ConfigurationProperties(prefix = "javadoop.database")public class DataBase {  String url;  String username;  String password;  // getters and setters}

這樣,就在 Spring 的容器中就自動(dòng)注冊(cè)了一個(gè)類型為 DataBase 的 bean 了,而且屬性都已經(jīng) set 好了。

在啟動(dòng)過程中動(dòng)態(tài)修改屬性值

這個(gè)我覺得都不需要太多介紹,用 Spring Boot 的應(yīng)該基本上都知道。

屬性配置有個(gè)覆蓋順序,也就是當(dāng)出現(xiàn)相同的 key 的時(shí)候,以哪里的值為準(zhǔn)。

啟動(dòng)參數(shù) > application-{env}.properties > application.properties

啟動(dòng)參數(shù)動(dòng)態(tài)設(shè)置屬性:

java -Djavadoop.database.password=admin4321 -jar app.jar

另外,還可以利用系統(tǒng)環(huán)境變量設(shè)置屬性,還可以指定隨機(jī)數(shù)等等,確實(shí)很靈活,不過沒什么用,就不介紹了。

總結(jié)

讀者如果想要更加深入地了解 Spring 的 Properties,需要去理解 Spring 的 Environment 接口相關(guān)的源碼。建議感興趣的讀者去翻翻源代碼看看

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 历史| 靖边县| 宁武县| 海盐县| 林口县| 马公市| 太康县| 遵义县| 平舆县| 藁城市| 昭平县| 广平县| 盱眙县| 舞钢市| 乌什县| 冕宁县| 灵武市| 江达县| 揭西县| 安化县| 石林| 嘉禾县| 宝山区| 阿坝| 新兴县| 武胜县| 承德市| 荔波县| 大同市| 修文县| 彭州市| 隆德县| 油尖旺区| 古田县| 临沭县| 余干县| 文安县| 大厂| 东乡| 汾西县| 姜堰市|