上一节使用了Spring的Environment、解析属性占位符来进行装配,不过Spring还有一种方法,就是使用Spring表达式语言,即SpEL,它能够实现其他装配技术难以做到的效果。
一、简介:
SpEL拥有很多特性,包括:
1、使用bean的ID来引用bean;
2、调用方法和访问对象的属性;
3、对值进行算数、关系和逻辑运算;
4、正则表达式匹配;
5、集合操作;
我们可以使用SpEL的基础表示式方便的实现我们想要的装配效果。
二、使用方法:
我们都知道,在使用属性占位符时,要使用${xxx}这种形式,如果要使用SpEL的话,那么要使用下面的形式:
#{xxx}
1、注入简单值:
注入一个String:
@Value("#{'happyhengWithSpEL'}")
private String name;
注入一个int:
@Value("#{18}")
private int age;
2、注入属性值:
当属性文件已经导入之后,那么就可以通过SpEL来得到属性值,方法为:
@Value("#{configProperties['name']}")
private String name;
//注意,上述configProperties即为Properties的id名称,Properties既可以通过下面的配置文件来生成的,也可以通过JavaConfig来生成
2.1、通过xml来生成Properties
不过这里推荐使用xml来进行配置,下面为我的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.happyheng"/>
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:property/enviroment.properties</value>
</list>
</property>
</bean>
</beans>
2.2、通过JavaConfig来生成Properties:
@Bean
public PropertiesFactoryBean configProperties(){
PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource("property/enviroment.properties"));
return bean;
}
可以看到,使用SpEL来得到配置文件比较繁琐,所以可以直接使用属性占位符来获取。
3、引用其它bean及其方法:
使用SpEL强大之处即其可以引用其它bean的变量或者方法:
3.1、引用其它bean的变量:
@Value("#{valuePeople.name}")
private String valuePeopleName;
注意valuePeople即为bean的id,name即为其变量,注意,如果ValuePeople的name为private,那么就会调用这个变量的getter方法
3.2、引用其它bean的方法:
@Value("#{valuePeople.toString()}")
private String valuePeopleToString;
一、简介:
SpEL拥有很多特性,包括:
1、使用bean的ID来引用bean;
2、调用方法和访问对象的属性;
3、对值进行算数、关系和逻辑运算;
4、正则表达式匹配;
5、集合操作;
我们可以使用SpEL的基础表示式方便的实现我们想要的装配效果。
二、使用方法:
我们都知道,在使用属性占位符时,要使用${xxx}这种形式,如果要使用SpEL的话,那么要使用下面的形式:
#{xxx}
1、注入简单值:
注入一个String:
@Value("#{'happyhengWithSpEL'}")
private String name;
注入一个int:
@Value("#{18}")
private int age;
2、注入属性值:
当属性文件已经导入之后,那么就可以通过SpEL来得到属性值,方法为:
@Value("#{configProperties['name']}")
private String name;
//注意,上述configProperties即为Properties的id名称,Properties既可以通过下面的配置文件来生成的,也可以通过JavaConfig来生成
2.1、通过xml来生成Properties
不过这里推荐使用xml来进行配置,下面为我的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.happyheng"/>
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:property/enviroment.properties</value>
</list>
</property>
</bean>
</beans>
2.2、通过JavaConfig来生成Properties:
@Bean
public PropertiesFactoryBean configProperties(){
PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource("property/enviroment.properties"));
return bean;
}
可以看到,使用SpEL来得到配置文件比较繁琐,所以可以直接使用属性占位符来获取。
3、引用其它bean及其方法:
使用SpEL强大之处即其可以引用其它bean的变量或者方法:
3.1、引用其它bean的变量:
@Value("#{valuePeople.name}")
private String valuePeopleName;
注意valuePeople即为bean的id,name即为其变量,注意,如果ValuePeople的name为private,那么就会调用这个变量的getter方法
3.2、引用其它bean的方法:
@Value("#{valuePeople.toString()}")
private String valuePeopleToString;
其即为调用id为valuePeople的bean的toString()方法,并将值注入到valuePeopleToString中。
4、此项目在github上的链接为:
https://github.com/happyheng/property-test