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