溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Properties怎么在Spring中使用

發(fā)布時(shí)間:2021-03-30 15:52:07 來源:億速云 閱讀:122 作者:Leah 欄目:編程語言

Properties怎么在Spring中使用?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

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ì)說。

@Autowired
private 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")
@Configuration
public 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 的版本:

@Bean
public 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 版本吧:

@Bean
public 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ù)庫連接信息就不一樣。

所以,在 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=admin
javadoop.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

看完上述內(nèi)容,你們掌握Properties怎么在Spring中使用的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI