Spring系统属性及spring.properties配置文件示例详解

 更新时间:2023年07月19日 09:18:20   作者:韩长奇  
spring中有一个SpringProperties类,来保存spring的系统属性,本文结合实例代码对Spring系统属性及spring.properties配置文件相关知识给大家介绍的非常详细,需要的朋友参考下吧

Spring系统属性、spring.properties配置文件

Spring系统属性

1、spring中有一个SpringProperties类,来保存spring的系统属性。

public final class SpringProperties {
	// 存放spring系统属性的配置文件
	private static final String PROPERTIES_RESOURCE_LOCATION = "spring.properties";
	// 使用Java的Properties保存spring的系统属性。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配置文件,会读取到配置文件中的属性并设置到spring的系统属性中
			if (url != null) {
				// 将配置文件加载到输入流中
				try (InputStream is = url.openStream()) {
					// 加载配置文件的键值对,添加到spring的系统属性中
					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属性,然后返回到jvm级别的系统属性。
	 * @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配置文件设置spring属性
		String value = localProperties.getProperty(key);
		// 如果没从spring的系统属性中获取到指定键的属性,会从Java的系统属性中获取
		if (value == null) {
			try {
				// 从Java的系统属性中获取指定键的属性值
				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路径下创建spring.properties配置文件,使用键值对的形式设置spring系统属性。
1)可以使用=(等号)或者:(冒号);
2)字符串键两边可以加空格(" ")、制表符(\t)、换页符(\f)即在换行后行尾有一个;
3)可以使用//将配置注释掉,只能使用单行注释。

spring.spel.ignore=true

3、spring在创建容器时会加载AbstractApplicationContext类,加载类时会加载静态属性。会加载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} 系统属性控制的布尔标志,指示 Spring 忽略 SpEL,即不初始化 SpEL 基础结构。
 * 默认值为 "false".
 */
// 静态变量,加载该类的时候就会加载静态变量。
// SpringProperties类中有静态代码块。会通过类加载器的getResource获取URL,参数为spring.properties
private static final boolean shouldIgnoreSpel = SpringProperties.getFlag("spring.spel.ignore");

4、加载SpringProperties类时,会加载静态代码块。会读取spring.properties配置文件中的键值对到spring的系统属性中。

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配置文件,会读取到配置文件中的属性并设置到spring的系统属性中
		if (url != null) {
			// 将配置文件加载到输入流中
			try (InputStream is = url.openStream()) {
				// 加载配置文件的键值对,添加到spring的系统属性中
				localProperties.load(is);
			}
		}
	}
	catch (IOException ex) {
		System.err.println("Could not load 'spring.properties' file from local classpath: " + ex);
	}
}

spring.properties配置文件

1、在spring项目的resources目录下创建spring.properties配置文件,在配置文件中添加键值对。

spring.spel.ignore=true

2、spring容器在创建是会加载AbstractApplicationContext类,会加载该类的静态属性。会加载SpringProperties类。

// 静态变量,加载该类的时候就会加载静态变量。
// SpringProperties类中有静态代码块。会通过类加载器的getResource获取URL,参数为spring.properties
private static final boolean shouldIgnoreSpel = SpringProperties.getFlag("spring.spel.ignore");

3、加载SpringProperties类时,会加载静态代码块。会读取spring.properties配置文件中的键值对到spring的系统属性中。

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配置文件,会读取到配置文件中的属性并设置到spring的系统属性中
		if (url != null) {
			// 将配置文件加载到输入流中
			try (InputStream is = url.openStream()) {
				// 加载配置文件的键值对,添加到spring的系统属性中
				localProperties.load(is);
			}
		}
	}
	catch (IOException ex) {
		System.err.println("Could not load 'spring.properties' file from local classpath: " + ex);
	}
}

4、创建一个main方法,创建一个容器,设置配置文件的路径,并刷新容器。

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配置文件中设置的对象。
2)打印出的名字是#{user.name}表示通过spring.properties设置的spring系统属性生效了。已经不支持spel了。
3)打印出的spring.spel.ignore的属性值为true

user = User{name='#{user.name}', age=21}
user2 = User{name='南宫仆射', age=22}
spring.spel.ignore = true

到此这篇关于spring.properties配置文件的文章就介绍到这了,更多相关spring.properties配置文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring中RedisTemplate的基本使用浅析

    Spring中RedisTemplate的基本使用浅析

    Spring Boot Data(数据) Redis中提供了RedisTemplate和StringRedisTemplate,其中StringRedisTemplate是RedisTemplate的子类,两个方法基本一致。本文介绍了Spring操作Redis的方法,需要的可以参考一下
    2023-02-02
  • java实现微信公众平台自定义菜单的创建示例

    java实现微信公众平台自定义菜单的创建示例

    这篇文章主要介绍了java实现微信公众平台自定义菜单的创建示例,需要的朋友可以参考下
    2014-04-04
  • JAVA读取文本文件内容实例代码

    JAVA读取文本文件内容实例代码

    这篇文章主要给大家介绍了关于JAVA读取文本文件内容的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-09-09
  • java spring mvc处理器映射器介绍

    java spring mvc处理器映射器介绍

    这篇文章主要介绍了java spring mvc处理器映射器,文章围绕equestMapping解析映射介绍展开源码内容,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-03-03
  • Linux系统卸载重装JDK的完整流程

    Linux系统卸载重装JDK的完整流程

    Linux系统有时候会默认使用OpenJDK版本,需要卸载后重新安装自己需要的JDK版本,下面这篇文章主要给大家介绍了关于Linux系统卸载重装JDK的完整流程,需要的朋友可以参考下
    2024-02-02
  • 带你了解Java常用类小结

    带你了解Java常用类小结

    今天带大家学习Java常用工具类,文中有非常详细的图文解说及代码示例,对正在学习java的小伙伴们很有帮助,需要的朋友可以参考下,希望能给你带来帮助
    2021-07-07
  • 双重检查锁定模式Java中的陷阱案例

    双重检查锁定模式Java中的陷阱案例

    这篇文章主要介绍了双重检查锁定模式Java中的陷阱,双重检查锁定(也叫做双重检查锁定优化)是一种软件设计模式,它的作用是减少延迟初始化在多线程环境下获取锁的次数,尤其是单例模式下比较突出,想具体了解的小伙伴可以参考下面文章内容,附呦详细的举例说明
    2021-10-10
  • SpringBoot整合mysql、postgres及sqlserver实现多数据源配置实战案例

    SpringBoot整合mysql、postgres及sqlserver实现多数据源配置实战案例

    在工作中业务的发展或业务数据隔离的场景下,通常需要一个项目中引入多个数据源,但SpringBoot默认的自动化配置是单数据源的,这篇文章主要给大家介绍了关于SpringBoot整合mysql、postgres及sqlserver实现多数据源配置的相关资料,需要的朋友可以参考下
    2023-12-12
  • Java8函数式接口Predicate用法示例详解

    Java8函数式接口Predicate用法示例详解

    这篇文章主要为大家介绍了Java8函数式接口Predicate用法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • Spring和Hibernate的整合操作示例

    Spring和Hibernate的整合操作示例

    这篇文章主要介绍了Spring和Hibernate的整合操作,结合实例形式详细分析了Spring和Hibernate的整合具体步骤、实现方法及相关操作注意事项,需要的朋友可以参考下
    2020-01-01

最新评论