最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring系統(tǒng)屬性及spring.properties配置文件示例詳解

 更新時間:2023年07月19日 09:18:20   作者:韓長奇  
spring中有一個SpringProperties類,來保存spring的系統(tǒng)屬性,本文結(jié)合實例代碼對Spring系統(tǒng)屬性及spring.properties配置文件相關(guān)知識給大家介紹的非常詳細,需要的朋友參考下吧

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());
	}
	/**
	 * 檢索給定屬性鍵的標(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配置文件,使用鍵值對的形式設(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)屬性控制的布爾標(biāo)志,指示 Spring 忽略 SpEL,即不初始化 SpEL 基礎(chǔ)結(jié)構(gòu)。
 * 默認(rèn)值為 "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)文章

  • Spring中RedisTemplate的基本使用淺析

    Spring中RedisTemplate的基本使用淺析

    Spring Boot Data(數(shù)據(jù)) Redis中提供了RedisTemplate和StringRedisTemplate,其中StringRedisTemplate是RedisTemplate的子類,兩個方法基本一致。本文介紹了Spring操作Redis的方法,需要的可以參考一下
    2023-02-02
  • java實現(xiàn)微信公眾平臺自定義菜單的創(chuàng)建示例

    java實現(xiàn)微信公眾平臺自定義菜單的創(chuàng)建示例

    這篇文章主要介紹了java實現(xiàn)微信公眾平臺自定義菜單的創(chuàng)建示例,需要的朋友可以參考下
    2014-04-04
  • JAVA讀取文本文件內(nèi)容實例代碼

    JAVA讀取文本文件內(nèi)容實例代碼

    這篇文章主要給大家介紹了關(guān)于JAVA讀取文本文件內(nèi)容的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • java?spring?mvc處理器映射器介紹

    java?spring?mvc處理器映射器介紹

    這篇文章主要介紹了java?spring?mvc處理器映射器,文章圍繞equestMapping解析映射介紹展開源碼內(nèi)容,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-03-03
  • Linux系統(tǒng)卸載重裝JDK的完整流程

    Linux系統(tǒng)卸載重裝JDK的完整流程

    Linux系統(tǒng)有時候會默認(rèn)使用OpenJDK版本,需要卸載后重新安裝自己需要的JDK版本,下面這篇文章主要給大家介紹了關(guān)于Linux系統(tǒng)卸載重裝JDK的完整流程,需要的朋友可以參考下
    2024-02-02
  • 帶你了解Java常用類小結(jié)

    帶你了解Java常用類小結(jié)

    今天帶大家學(xué)習(xí)Java常用工具類,文中有非常詳細的圖文解說及代碼示例,對正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下,希望能給你帶來幫助
    2021-07-07
  • 雙重檢查鎖定模式Java中的陷阱案例

    雙重檢查鎖定模式Java中的陷阱案例

    這篇文章主要介紹了雙重檢查鎖定模式Java中的陷阱,雙重檢查鎖定(也叫做雙重檢查鎖定優(yōu)化)是一種軟件設(shè)計模式,它的作用是減少延遲初始化在多線程環(huán)境下獲取鎖的次數(shù),尤其是單例模式下比較突出,想具體了解的小伙伴可以參考下面文章內(nèi)容,附呦詳細的舉例說明
    2021-10-10
  • SpringBoot整合mysql、postgres及sqlserver實現(xiàn)多數(shù)據(jù)源配置實戰(zhàn)案例

    SpringBoot整合mysql、postgres及sqlserver實現(xiàn)多數(shù)據(jù)源配置實戰(zhàn)案例

    在工作中業(yè)務(wù)的發(fā)展或業(yè)務(wù)數(shù)據(jù)隔離的場景下,通常需要一個項目中引入多個數(shù)據(jù)源,但SpringBoot默認(rèn)的自動化配置是單數(shù)據(jù)源的,這篇文章主要給大家介紹了關(guān)于SpringBoot整合mysql、postgres及sqlserver實現(xiàn)多數(shù)據(jù)源配置的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • Java8函數(shù)式接口Predicate用法示例詳解

    Java8函數(shù)式接口Predicate用法示例詳解

    這篇文章主要為大家介紹了Java8函數(shù)式接口Predicate用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Spring和Hibernate的整合操作示例

    Spring和Hibernate的整合操作示例

    這篇文章主要介紹了Spring和Hibernate的整合操作,結(jié)合實例形式詳細分析了Spring和Hibernate的整合具體步驟、實現(xiàn)方法及相關(guān)操作注意事項,需要的朋友可以參考下
    2020-01-01

最新評論

余姚市| 兴文县| 云浮市| 遵义市| 邢台县| 邹平县| 宁河县| 广宗县| 依安县| 册亨县| 沾益县| 海原县| 南召县| 乳山市| 镇原县| 潞城市| 东城区| 广河县| 社会| 合阳县| 博湖县| 祁门县| 喀喇沁旗| 姜堰市| 泰宁县| 阿鲁科尔沁旗| 宁蒗| 太康县| 漳州市| 彰化市| 灵台县| 子长县| 浦东新区| 高密市| 志丹县| 常宁市| 内乡县| 双江| 吉水县| 玉门市| 逊克县|