说说如何在 Spring 框架中用 SpEL 表达式
在 Spring 框架的 XML 配置方式或者者注解配置方式中,我们都可以用 SpEL 表达式,它们的语法都是 #{表达式}
。
1 基于 XML 配置
在 XML 配置中,我们可以通过 SpEL 表达式为 Bean 的属性或者构造函数入参注入动态值:
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="systemPropertiesBean" class="net.deniro.spring4.spel.SystemPropertiesBean" p:osName="#{systemProperties['os.name']}" p:classPath="#{systemProperties['java.class.path']}" /></beans>
在此,我们通过表达式动态地为 SystemPropertiesBean 注入 osName(操作系统名)与 classPath(类路径)属性。
SystemPropertiesBean.java
public class SystemPropertiesBean { private String osName; private String classPath; public void setOsName(String osName) { this.osName = osName; } public String getOsName() { return osName; } public void setClassPath(String classPath) { this.classPath = classPath; } public String getClassPath() { return classPath; } @Override public String toString() { return "SystemProperties{" + "osName='" + osName + '\'' + ", classPath='" + classPath + '\'' + '}'; }}
单元测试:
ApplicationContext context = new ClassPathXmlApplicationContext("spring9-5.xml");SystemPropertiesBean systemPropertiesBean = (SystemPropertiesBean) context .getBean("systemPropertiesBean");System.out.println("systemPropertiesBean:"+systemPropertiesBean);
输出结果:
systemPropertiesBean:SystemProperties{osName=’Windows 7′, classPath=’C:\Program Files (x86)\IntelliJ IDEA …
可以通过表达式 #{Bean名称.属性名称}
的方式来引使用其它 Bean 已经定义好的属性:
<bean id="quoteBean" class="net.deniro.spring4.spel.QuoteBean" p:osName="#{systemPropertiesBean.osName}" />
QuoteBean.java:
public class QuoteBean { private String osName; public void setOsName(String osName) { this.osName = osName; } public String getOsName() { return osName; } @Override public String toString() { return "QuoteBean{" + "osName='" + osName + '\'' + '}'; }}
单元测试:
ApplicationContext context = new ClassPathXmlApplicationContext("spring9-5.xml");QuoteBean quoteBean = (QuoteBean) context .getBean("quoteBean");System.out.println("quoteBean:"+quoteBean);
输出结果:
quoteBean:QuoteBean{osName=’Windows 7′}
2 基于注解配置
可以用 @Value
注解标注在类属性、方法和构造函数上。一般使用来从配置文件中加载参数值。
标注了 @Value
注解的类:
@Component(value = "SystemParams")public class SystemParams { @Value("driverClassName") private String driverClassName; public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } @Override public String toString() { return "SystemParams{" + "driverClassName='" + driverClassName + '\'' + '}'; }}
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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="net.deniro.spring4.spel"/> <util:properties id="properties" location="classpath:system.properties"/> <context:property-placeholder properties-ref="properties"/></beans>
- 引入了 util 命名空间,通过
util:properties
直接从 classpath 中加载配置文件。 - 还通过
context:property-placeholder
指定配置名,从而简化了 @Value 中的 SpEL 表达式。假如这里没有指定,那么 SpEL 表达式必需定义为#{properties['driverClassName']
。
单元测试:
ApplicationContext context = new ClassPathXmlApplicationContext("spring9-5.xml");SystemParams systemParams = (SystemParams) context .getBean("SystemParams");System.out.println(systemParams);
输出结果:
SystemParams{driverClassName=’driverClassName’}
是不是很简单呀 O(∩_∩)O哈哈~
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 说说如何在 Spring 框架中用 SpEL 表达式
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 说说如何在 Spring 框架中用 SpEL 表达式