Spring系統(tǒng)屬性及spring.properties配置文件示例詳解
Spring系統(tǒng)屬性、spring.properties配置文件
Spring系統(tǒng)屬性
1、spring中有一個(gè)SpringProperties類,來(lái)保存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,可以理解為一個(gè)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配置文件,會(huì)讀取到配置文件中的屬性并設(shè)置到spring的系統(tǒng)屬性中 if (url != null) { // 將配置文件加載到輸入流中 try (InputStream is = url.openStream()) { // 加載配置文件的鍵值對(duì),添加到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級(jí)別的系統(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的配置文件,可以通過(guò)spring.properties配置文件設(shè)置spring屬性 String value = localProperties.getProperty(key); // 如果沒(méi)從spring的系統(tǒng)屬性中獲取到指定鍵的屬性,會(huì)從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()); } /** * 檢索給定屬性鍵的標(biāo)志。 * * 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配置文件,使用鍵值對(duì)的形式設(shè)置spring系統(tǒng)屬性。
1)可以使用=(等號(hào))或者:(冒號(hào));
2)字符串鍵兩邊可以加空格(" ")、制表符(\t)、換頁(yè)符(\f)即在換行后行尾有一個(gè);
3)可以使用//將配置注釋掉,只能使用單行注釋。
spring.spel.ignore=true
3、spring在創(chuàng)建容器時(shí)會(huì)加載AbstractApplicationContext類,加載類時(shí)會(huì)加載靜態(tài)屬性。會(huì)加載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)屬性控制的布爾標(biāo)志,指示 Spring 忽略 SpEL,即不初始化 SpEL 基礎(chǔ)結(jié)構(gòu)。 * 默認(rèn)值為 "false". */ // 靜態(tài)變量,加載該類的時(shí)候就會(huì)加載靜態(tài)變量。 // SpringProperties類中有靜態(tài)代碼塊。會(huì)通過(guò)類加載器的getResource獲取URL,參數(shù)為spring.properties private static final boolean shouldIgnoreSpel = SpringProperties.getFlag("spring.spel.ignore");
4、加載SpringProperties類時(shí),會(huì)加載靜態(tài)代碼塊。會(huì)讀取spring.properties配置文件中的鍵值對(duì)到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配置文件,會(huì)讀取到配置文件中的屬性并設(shè)置到spring的系統(tǒng)屬性中 if (url != null) { // 將配置文件加載到輸入流中 try (InputStream is = url.openStream()) { // 加載配置文件的鍵值對(duì),添加到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項(xiàng)目的resources目錄下創(chuàng)建spring.properties配置文件,在配置文件中添加鍵值對(duì)。
spring.spel.ignore=true
2、spring容器在創(chuàng)建是會(huì)加載AbstractApplicationContext類,會(huì)加載該類的靜態(tài)屬性。會(huì)加載SpringProperties類。
// 靜態(tài)變量,加載該類的時(shí)候就會(huì)加載靜態(tài)變量。 // SpringProperties類中有靜態(tài)代碼塊。會(huì)通過(guò)類加載器的getResource獲取URL,參數(shù)為spring.properties private static final boolean shouldIgnoreSpel = SpringProperties.getFlag("spring.spel.ignore");
3、加載SpringProperties類時(shí),會(huì)加載靜態(tài)代碼塊。會(huì)讀取spring.properties配置文件中的鍵值對(duì)到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配置文件,會(huì)讀取到配置文件中的屬性并設(shè)置到spring的系統(tǒng)屬性中 if (url != null) { // 將配置文件加載到輸入流中 try (InputStream is = url.openStream()) { // 加載配置文件的鍵值對(duì),添加到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)建一個(gè)main方法,創(chuàng)建一個(gè)容器,設(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)打印的兩個(gè)對(duì)象是在applicationcontext.xml配置文件中設(shè)置的對(duì)象。
2)打印出的名字是#{user.name}表示通過(guò)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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java累加和校驗(yàn)實(shí)現(xiàn)方式16進(jìn)制(推薦)
下面小編就為大家?guī)?lái)一篇java累加和校驗(yàn)實(shí)現(xiàn)方式16進(jìn)制(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11Java中synchronized關(guān)鍵字的使用和原理詳解
這篇文章主要介紹了Java中synchronized關(guān)鍵字的使用和原理詳解,JVM 是通過(guò)進(jìn)入、退出 對(duì)象監(jiān)視器(Monitor)來(lái)實(shí)現(xiàn)對(duì)方法、同步塊的同步的,而對(duì)象監(jiān)視器的本質(zhì)依賴于底層操作系統(tǒng)的互斥鎖實(shí)現(xiàn),需要的朋友可以參考下2023-09-09Spring MVC入門(mén)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Spring MVC入門(mén),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案
這篇文章主要介紹了Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解
這篇文章主要介紹了Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Mybatis?Plus?QueryWrapper復(fù)合用法詳解
這篇文章主要介紹了Mybatis?Plus?QueryWrapper復(fù)合用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01Springboot基于websocket實(shí)現(xiàn)簡(jiǎn)單在線聊天功能
這篇文章主要介紹了Springboot基于websocket實(shí)現(xiàn)簡(jiǎn)單在線聊天功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06SpringCache 分布式緩存的實(shí)現(xiàn)方法(規(guī)避redis解鎖的問(wèn)題)
這篇文章主要介紹了SpringCache 分布式緩存的實(shí)現(xiàn)方法(規(guī)避redis解鎖的問(wèn)題),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11