说说如何在 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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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.xsdhttp://www.springframework.org/schema/utilhttp://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哈哈~
说说如何在 Spring 框架中使用 SpEL 表达式相关推荐
- Spring Aop中解析spel表达式,实现更灵活的功能
前言 在Spring Aop中,我们可以拿到拦截方法的参数,如果能结合spel表达式,就能实现更加灵活的功能.典型的实现有Spring的缓存注解: @Cacheable(value = "u ...
- Spring框架中的设计模式(一)
设计模式有助于遵循良好的编程实践.作为最流行的Web框架之一的Spring框架也使用其中的一些. 本文将介绍Spring Framework中使用的设计模式.这是5篇专题文章的第一部分.这次我们将发现 ...
- 如何在 Spring 生态中玩转 RocketMQ?
作者 | 通融.洛夜 来源 | 阿里巴巴云原生公众号 RocketMQ 作为业务消息的首选,在消息和流处理领域被广泛应用.而微服务生态 Spring 框架也是业务开发中最受欢迎的框架,两者的完美契合使 ...
- 如何在Spring生态中玩转RocketMQ?
简介: RocketMQ作为业务消息的首选,在消息和流处理领域被广泛应用.而微服务生态Spring框架也是业务开发中最受欢迎的框架,两者的完美契合使得RocketMQ成为Spring Messagin ...
- 如何在Spring Boot中使用TDD写出高质量的接口
本文发布于专栏Effective Java,如果您觉得看完之后对你有所帮助,欢迎订阅本专栏,也欢迎您将本专栏分享给您身边的工程师同学. 之前在<如何说服你的同事使用TDD>中介绍了为什么要 ...
- 在Spring 框架中如何更有效的使用JDBC?
使用Spring JDBC 框架,资源管理以及错误处理的代价都会减轻.开发人员只需通过statements 和queries 语句从数据库中存取数据.Spring 框架中通过使用模板类能更有效的使用J ...
- 如何在Spring boot中修改默认端口
文章目录 介绍 使用Property文件 在程序中指定 使用命令行参数 值生效的顺序 如何在Spring boot中修改默认端口 介绍 Spring boot为应用程序提供了很多属性的默认值.但是有时 ...
- Java访问静态常量_Java如何在Spring EL中访问静态方法或常量?
在这个例子中,您将学习如何使用Spring Expression Language访问类范围的方法或常量.要访问类范围的方法或常量T(),例如,您将需要使用Spring EL的运算符T(java.la ...
- 在Spring框架中使用SQL存储过程
Spring框架也支持对SQL存储过程的调用,SQL存储过程是一组预先定义好的SQL语句,并存储到数据库管理系统中,外部程序可以直接调用执行.本课主要讨论在Spring框架中应用程序如何调用MySQL ...
最新文章
- Future有返回值的线程
- Focal Loss 的Pytorch
- 利用计算机漏洞犯罪,利用漏洞非法谋利2000元怎么处罚
- iOS开发系列文章(持续更新……)
- Flask入门系列(转载)
- 听说,阿里“拆中台”了?
- Laravel核心解读--HTTP内核
- java 建立ssh隧道_如何使用IntelliJ和JDBC SSH隧道并连接到数据库?
- linux NTP服务
- tiny core linux 7.1,极度简约 最小 Linux 发行版 Tiny Core Linux 7.1 发布
- 软件测试中测试用例的简单案例
- CentOS7安装搜狗输入法
- 如何才能更持久系列之——如何才能准确测量APP的功耗
- SpringBoot入门项目——holleWorld
- [Android] Joystick游戏手柄开发
- 用html做相册需要什么,如何快速有效的生成HTML相册?
- Mac Redis开机自启动
- 在线生成各种印章式图片
- 安徽阜阳育英计算机学校,阜阳育英中学
- QQ邮箱设置企业邮箱别名邮箱