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配置文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java 中类似js encodeURIComponent 函数的实现案例

    java 中类似js encodeURIComponent 函数的实现案例

    这篇文章主要介绍了java 中类似js encodeURIComponent 函数的实现案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • springboot访问template下的html页面的实现配置

    springboot访问template下的html页面的实现配置

    这篇文章主要介绍了springboot访问template下的html页面的实现配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Java深入浅出讲解多线程的概念到使用

    Java深入浅出讲解多线程的概念到使用

    哈哈!经过一个阶段的学习,Java基础知识学习终于到多线程了!Java多线程以及后面互斥锁的概念都是Java基础学习的难点,所以我做了一个总结,希望对大家也有帮助
    2022-05-05
  • java接口自动化测试框架及断言详解

    java接口自动化测试框架及断言详解

    这篇文章主要介绍了java接口自动化测试框架及断言详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • Mybatis新手教程之简单入门

    Mybatis新手教程之简单入门

    这篇文章主要给大家介绍了关于Mybatis新手教程之简单入门的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-02-02
  • 深入浅出讲解Java比较器及数学常用类

    深入浅出讲解Java比较器及数学常用类

    这篇文章主要介绍了深入浅出讲解Java比较器及数学常用类,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • grails不能运行fork模式解决方法

    grails不能运行fork模式解决方法

    这篇文章主要介绍了如何解决grails2.3.2中不能运行fork模式的异常,大家参考使用吧
    2013-11-11
  • feign的ribbon超时配置和hystrix的超时配置说明

    feign的ribbon超时配置和hystrix的超时配置说明

    这篇文章主要介绍了feign的ribbon超时配置和hystrix的超时配置说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • spring-boot使用Admin监控应用的方法

    spring-boot使用Admin监控应用的方法

    本篇文章主要介绍了spring-boot使用Admin监控应用的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • Spring 使用注解方式进行事务管理配置方式

    Spring 使用注解方式进行事务管理配置方式

    本篇文章主要介绍了Spring 使用注解方式进行事务管理配置方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04

最新评论