Spring源码(一):Spring支持Spel表达式语言的配置

Spring中的表达式语言(简称spel)是一种强大的表达式语言。

Spring中spel的使用

注意:spring中默认支持spel。

1、在AbstractApplicationContext类中有一个static、final修饰的变量,默认为false。

// 系统属性控制的布尔标志:默认为false,表示不忽略(支持spel);true表示忽略(不支持spel)
// getFlag方法中会通过System.getProperty(key)获取变量的值进行判断。
private static final boolean shouldIgnoreSpel = SpringProperties.getFlag("spring.spel.ignore");

2、获取"spring.spel.ignore"的标识。true表示忽略spel,即不支持spel;false表示不忽略spel,即支持spel。

// 检索给定属性键的标志。
public static boolean getFlag(String key) {
	return Boolean.parseBoolean(getProperty(key));
}

3、SpringProperties类中的getProperties方法。
1)先获取spring的系统属性。可以通过spring.properties配置文件设置spring的系统属性。
2)如果没获取到spring的系统属性,会回去Java的系统属性。如果也没获取到,返回空。

// 检索给定键的属性值,首先检查本地Spring属性,然后返回到jvm级别的系统属性。
// @Nullable注解用在方法上,表示该方法的返回可以为空
@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;
}

4、在applicationContext.xml配置文件中定义bean对象

<bean id="user" class="com.project.entity.User">
	<property name="age" value="21"></property>
	<!-- 使用#{},从配置文件中读取name的值-->
	<property name="name" value="#{user.name}"></property>
</bean>

5、在main方法中创建容器

public static void main(String[] args) {
	String shouldIgnoreSpel = System.getProperty("spring.spel.ignore");
	System.out.println("shouldIgnoreSpel = " + shouldIgnoreSpel);
	ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext();
	classPathXmlApplicationContext.setConfigLocation("classpath*:/*.xml");
	classPathXmlApplicationContext.refresh();
	User user = (User) classPathXmlApplicationContext.getBean("user");
	System.out.println("user = " + user);
}

6、控制台输出如下:
1)System.getProperty(“spring.spel.ignore”)获取到的是null,只要该值不是true,spring就支持spel。
2)我没有在配置文件中添加user.name的值,解析占位符时,给对象的属性值赋值null字符串。表明spring是支持spel的

shouldIgnoreSpel = null
user = User{name='null', age=21}

修改Spring的默认配置,不再支持spel

1、修改spring是否支持spel有两种方式
方式一:在resource目录下新建spring.properties配置文件,并设置spring.spel.ignore属性值。该方式设置的是Spring的系统属性。
spring.spel.ignore配置文件中的代码

spring.spel.ignore=true

方式二:在main方法中给Java系统属性添加spring.spel.ignore的值为true。(其他代码不变)

public static void main(String[] args) {
	// 必须在创建spring容器之前,否则不会生效。
	System.setProperty("spring.spel.ignore", "true");
	String shouldIgnoreSpel = System.getProperty("spring.spel.ignore");
	System.out.println("shouldIgnoreSpel = " + shouldIgnoreSpel);
	
	ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext();
	classPathXmlApplicationContext.setConfigLocation("classpath*:/*.xml");
	classPathXmlApplicationContext.refresh();
	User user = (User) classPathXmlApplicationContext.getBean("user");
	System.out.println("user = " + user);
}

2、控制台输出如下:
1)System.getProperty(“spring.spel.ignore”)获取到的是true,表示spring不再支持spel。
2)spring在给bean对象属性赋值时并没有解析#{user.name},直接将它作为字符串赋值给对象属性。

spring.spel.ignore = true
user = User{name='#{user.name}', age=21}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值