Spring系統(tǒng)屬性及spring.properties配置文件示例詳解
Spring系統(tǒng)屬性、spring.properties配置文件
Spring系統(tǒng)屬性
1、spring中有一個SpringProperties類,來保存spring的系統(tǒng)屬性。
public final class SpringProperties {
// 存放spring系統(tǒng)屬性的配置文件
private static final String PROPERTIES_RESOURCE_LOCATION = "spring.properties";
// 使用Java的Properties保存spring的系統(tǒng)屬性。Properties類繼承了HashTable,可以理解為一個map
public static Properties localProperties = new Properties();
public static Properties getLocalProperties() {
return localProperties;
}
static {
try {
// 獲取SpringProperties類的類加載器
ClassLoader cl = SpringProperties.class.getClassLoader();
// 使用類加載器將spring.properties配置文件解析成URL
URL url = (cl != null ? cl.getResource(PROPERTIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResource(PROPERTIES_RESOURCE_LOCATION));
// 如果獲取到spring.properties配置文件,會讀取到配置文件中的屬性并設(shè)置到spring的系統(tǒng)屬性中
if (url != null) {
// 將配置文件加載到輸入流中
try (InputStream is = url.openStream()) {
// 加載配置文件的鍵值對,添加到spring的系統(tǒng)屬性中
localProperties.load(is);
}
}
}
catch (IOException ex) {
System.err.println("Could not load 'spring.properties' file from local classpath: " + ex);
}
}
private SpringProperties() {
}
/**
* Programmatically set a local property, overriding an entry in the
* {@code spring.properties} file (if any).
* @param key the property key
* @param value the associated property value, or {@code null} to reset it
*/
public static void setProperty(String key, @Nullable String value) {
if (value != null) {
localProperties.setProperty(key, value);
}
else {
localProperties.remove(key);
}
}
/**
* 檢索給定鍵的屬性值,首先檢查本地Spring屬性,然后返回到j(luò)vm級別的系統(tǒng)屬性。
* @Nullable 注解用在方法上,表示該方法可以返回空
*
* Retrieve the property value for the given key, checking local Spring
* properties first and falling back to JVM-level system properties.
* @param key the property key
* @return the associated property value, or {@code null} if none found
*/
@Nullable
public static String getProperty(String key) {
// 獲取spring的配置文件,可以通過spring.properties配置文件設(shè)置spring屬性
String value = localProperties.getProperty(key);
// 如果沒從spring的系統(tǒng)屬性中獲取到指定鍵的屬性,會從Java的系統(tǒng)屬性中獲取
if (value == null) {
try {
// 從Java的系統(tǒng)屬性中獲取指定鍵的屬性值
value = System.getProperty(key);
}
catch (Throwable ex) {
System.err.println("Could not retrieve system property '" + key + "': " + ex);
}
}
return value;
}
/**
* Programmatically set a local flag to "true", overriding an
* entry in the {@code spring.properties} file (if any).
* @param key the property key
*/
public static void setFlag(String key) {
localProperties.put(key, Boolean.TRUE.toString());
}
/**
* 檢索給定屬性鍵的標志。
*
* Retrieve the flag for the given property key.
* @param key the property key
* @return {@code true} if the property is set to "true",
* {@code} false otherwise
*/
public static boolean getFlag(String key) {
return Boolean.parseBoolean(getProperty(key));
}
}2、在resoreces路徑下創(chuàng)建spring.properties配置文件,使用鍵值對的形式設(shè)置spring系統(tǒng)屬性。
1)可以使用=(等號)或者:(冒號);
2)字符串鍵兩邊可以加空格(" ")、制表符(\t)、換頁符(\f)即在換行后行尾有一個;
3)可以使用//將配置注釋掉,只能使用單行注釋。
spring.spel.ignore=true
3、spring在創(chuàng)建容器時會加載AbstractApplicationContext類,加載類時會加載靜態(tài)屬性。會加載SpringProperties類。AbstractApplicationContext類中的代碼:
/**
* Boolean flag controlled by a {@code spring.spel.ignore} system property that instructs Spring to
* ignore SpEL, i.e. to not initialize the SpEL infrastructure.
* 由 {@code spring.spel.ignore} 系統(tǒng)屬性控制的布爾標志,指示 Spring 忽略 SpEL,即不初始化 SpEL 基礎(chǔ)結(jié)構(gòu)。
* 默認值為 "false".
*/
// 靜態(tài)變量,加載該類的時候就會加載靜態(tài)變量。
// SpringProperties類中有靜態(tài)代碼塊。會通過類加載器的getResource獲取URL,參數(shù)為spring.properties
private static final boolean shouldIgnoreSpel = SpringProperties.getFlag("spring.spel.ignore");4、加載SpringProperties類時,會加載靜態(tài)代碼塊。會讀取spring.properties配置文件中的鍵值對到spring的系統(tǒng)屬性中。
static {
try {
// 獲取SpringProperties類的類加載器
ClassLoader cl = SpringProperties.class.getClassLoader();
// 使用類加載器將spring.properties配置文件解析成URL
URL url = (cl != null ? cl.getResource(PROPERTIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResource(PROPERTIES_RESOURCE_LOCATION));
// 如果獲取到spring.properties配置文件,會讀取到配置文件中的屬性并設(shè)置到spring的系統(tǒng)屬性中
if (url != null) {
// 將配置文件加載到輸入流中
try (InputStream is = url.openStream()) {
// 加載配置文件的鍵值對,添加到spring的系統(tǒng)屬性中
localProperties.load(is);
}
}
}
catch (IOException ex) {
System.err.println("Could not load 'spring.properties' file from local classpath: " + ex);
}
}spring.properties配置文件
1、在spring項目的resources目錄下創(chuàng)建spring.properties配置文件,在配置文件中添加鍵值對。
spring.spel.ignore=true
2、spring容器在創(chuàng)建是會加載AbstractApplicationContext類,會加載該類的靜態(tài)屬性。會加載SpringProperties類。
// 靜態(tài)變量,加載該類的時候就會加載靜態(tài)變量。
// SpringProperties類中有靜態(tài)代碼塊。會通過類加載器的getResource獲取URL,參數(shù)為spring.properties
private static final boolean shouldIgnoreSpel = SpringProperties.getFlag("spring.spel.ignore");3、加載SpringProperties類時,會加載靜態(tài)代碼塊。會讀取spring.properties配置文件中的鍵值對到spring的系統(tǒng)屬性中。
static {
try {
// 獲取SpringProperties類的類加載器
ClassLoader cl = SpringProperties.class.getClassLoader();
// 使用類加載器將spring.properties配置文件解析成URL
URL url = (cl != null ? cl.getResource(PROPERTIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResource(PROPERTIES_RESOURCE_LOCATION));
// 如果獲取到spring.properties配置文件,會讀取到配置文件中的屬性并設(shè)置到spring的系統(tǒng)屬性中
if (url != null) {
// 將配置文件加載到輸入流中
try (InputStream is = url.openStream()) {
// 加載配置文件的鍵值對,添加到spring的系統(tǒng)屬性中
localProperties.load(is);
}
}
}
catch (IOException ex) {
System.err.println("Could not load 'spring.properties' file from local classpath: " + ex);
}
}4、創(chuàng)建一個main方法,創(chuàng)建一個容器,設(shè)置配置文件的路徑,并刷新容器。
public class ProjectApplication {
private ProjectApplication() {}
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext();
classPathXmlApplicationContext.setConfigLocation("classpath*:/*.xml");
classPathXmlApplicationContext.refresh();
Properties springProperties = SpringProperties.getLocalProperties();
for (Object springPropertyKey : springProperties.keySet()) {
System.out.println(springPropertyKey + " = " + springProperties.getProperty((String) springPropertyKey));
}
}
}5、控制太輸出。
1)打印的兩個對象是在applicationcontext.xml配置文件中設(shè)置的對象。
2)打印出的名字是#{user.name}表示通過spring.properties設(shè)置的spring系統(tǒng)屬性生效了。已經(jīng)不支持spel了。
3)打印出的spring.spel.ignore的屬性值為true
user = User{name='#{user.name}', age=21}
user2 = User{name='南宮仆射', age=22}
spring.spel.ignore = true到此這篇關(guān)于spring.properties配置文件的文章就介紹到這了,更多相關(guān)spring.properties配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中synchronized關(guān)鍵字的使用和原理詳解
這篇文章主要介紹了Java中synchronized關(guān)鍵字的使用和原理詳解,JVM 是通過進入、退出 對象監(jiān)視器(Monitor)來實現(xiàn)對方法、同步塊的同步的,而對象監(jiān)視器的本質(zhì)依賴于底層操作系統(tǒng)的互斥鎖實現(xiàn),需要的朋友可以參考下2023-09-09
Spring MVC入門_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Spring MVC入門,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案
這篇文章主要介紹了Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
Mybatis?Plus?QueryWrapper復(fù)合用法詳解
這篇文章主要介紹了Mybatis?Plus?QueryWrapper復(fù)合用法詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01
Springboot基于websocket實現(xiàn)簡單在線聊天功能
這篇文章主要介紹了Springboot基于websocket實現(xiàn)簡單在線聊天功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
SpringCache 分布式緩存的實現(xiàn)方法(規(guī)避redis解鎖的問題)
這篇文章主要介紹了SpringCache 分布式緩存的實現(xiàn)方法(規(guī)避redis解鎖的問題),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11

