SpringBoot | 第十一章:Spring中application.properties用法大全
對 Spring 裡面的 Properties 不理解的開發者可能會覺得有點亂,主要是因為配置方式很多種,使用方式也很多種。
本文不是原理分析、原始碼分析文章,只是希望可以幫助讀者更好地理解和使用 Spring Properties。
Properties 的使用
先來看看 Properties 是怎麼使用的,Spring 中常用的有以下幾種使用方式:
1. 在 xml 配置檔案中使用
即自動替換 ${} 裡面的值。
<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 介面來獲取屬性值,親測只有使用註解 @PropertySource 的時候可以用,否則會得到 null,至於怎麼配置,下面馬上就會說。
@Autowired private Environment env; public String getUrl() { return env.getProperty("javadoop.jdbc.url"); }
注:如果是 Spring Boot 的 application.properties 註冊的,那也是可以的。
Properties 配置
前面我們說了怎麼使用我們配置的 Properties,那麼該怎麼配置呢?Spring 提供了很多種配置方式。
1. 通過 xml 配置
下面這個是最常用的配置方式了,很多專案都是這麼寫的:
<context:property-placeholder location="classpath:sys.properties" />
2. 通過 @PropertySource 配置
前面的通過 xml 配置非常常用,但是如果你也有一種要消滅所有 xml 配置檔案的衝動的話,你應該使用以下方式:
#配置檔案jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bookstore
package com.timo.propertySource;
import com.timo.profile.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@PropertySource("classpath:jdbc.properties")
public class AppConfig {
//這個Environment是自動注入的:
@Autowired
private Environment env;
@Bean
public User user(){
User user = new User();
user.setName(env.getProperty("jdbc.className"));
return user ;
}
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(AppConfig.class);
applicationContext.refresh();
String name = applicationContext.getBean(User.class).getName();
System.out.println("name="+name);
}
}
注:@PropertySource 在這裡必須搭配 @Configuration 來使用,具體不展開說了。
3. PropertyPlaceholderConfigurer
如果讀者見過這個,也不必覺得奇怪,在 Spring 3.1 之前,經常就是這麼使用的:
<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>
當然,我們也可以用相應的 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 的時候,引入了 PropertySourcesPlaceholderConfigurer,這是一個新的類,注意看和之前的 PropertyPlaceholderConfigurer 在名字上多了一個 Sources,所屬的包也不一樣,它在 Spring-Context 包中。
在配置上倒是沒有什麼區別:
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:sys.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<!-- 這裡可以配置一些屬性 -->
</bean>
也來一個 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 相關
Spring Boot 真的是好東西,開箱即用的感覺實在是太好了。這裡簡單介紹下相關的內容。
快速生成一個 Spring Boot 專案:
https://start.spring.io/
application.properties
我們每個專案都預設有一個 application.properties 檔案,這個配置檔案不需要像前面說的那樣進行註冊,Spring Boot 會幫我們自動註冊。
當然,也許你想換個名字也是可以的,在啟動的時候指定你的檔名字就可以了:
java -Dspring.config.location=classpath:sys.properties -jar app.jar
application-{env}.properties
為了給不同的環境指定不同的配置,我們會用到這個。比如測試環境和生產環境的資料庫連線資訊就不一樣。
所以,在 application.properties 的基礎上,我們還需要新建 application-dev.properties 和 application-prd.properties,用於配置環境相關的資訊,然後啟動的時候指定環境。
java -Dspring.profiles.active=prd -jar app.jar
結果就是,application.properties 和 application-prd.properties 兩個檔案中的配置都會註冊進去,如果有重複的 key,application-prd.properties 檔案中的優先順序較高。
@ConfigurationProperties
這個註解是 Spring Boot 中才有的。即使大家不使用這個註解,大家也可能會在開源專案中看到這個,這裡簡單介紹下。
來一個例子直觀一些。按照之前說的,在配置檔案中填入下面的資訊,你可以選擇寫入 application.properties 也可以用第一節介紹的方法。
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 的容器中就自動註冊了一個型別為 DataBase 的 bean 了,而且屬性都已經 set 好了。
在啟動過程中動態修改屬性值
這個我覺得都不需要太多介紹,用 Spring Boot 的應該基本上都知道。
屬性配置有個覆蓋順序,也就是當出現相同的 key 的時候,以哪裡的值為準。
啟動引數 > application-{env}.properties > application.properties
啟動引數動態設定屬性:
java -Djavadoop.database.password=admin4321 -jar app.jar
另外,還可以利用系統環境變數設定屬性,還可以指定隨機數等等,確實很靈活,不過沒什麼用,就不介紹了。