在 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>
复制代码
  1. 引入了 util 命名空间,通过 util:properties 直接从 classpath 中加载配置文件。
  2. 还通过 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 表达式相关推荐

  1. Spring Aop中解析spel表达式,实现更灵活的功能

    前言 在Spring Aop中,我们可以拿到拦截方法的参数,如果能结合spel表达式,就能实现更加灵活的功能.典型的实现有Spring的缓存注解: @Cacheable(value = "u ...

  2. Spring框架中的设计模式(一)

    设计模式有助于遵循良好的编程实践.作为最流行的Web框架之一的Spring框架也使用其中的一些. 本文将介绍Spring Framework中使用的设计模式.这是5篇专题文章的第一部分.这次我们将发现 ...

  3. 如何在 Spring 生态中玩转 RocketMQ?

    作者 | 通融.洛夜 来源 | 阿里巴巴云原生公众号 RocketMQ 作为业务消息的首选,在消息和流处理领域被广泛应用.而微服务生态 Spring 框架也是业务开发中最受欢迎的框架,两者的完美契合使 ...

  4. 如何在Spring生态中玩转RocketMQ?

    简介: RocketMQ作为业务消息的首选,在消息和流处理领域被广泛应用.而微服务生态Spring框架也是业务开发中最受欢迎的框架,两者的完美契合使得RocketMQ成为Spring Messagin ...

  5. 如何在Spring Boot中使用TDD写出高质量的接口

    本文发布于专栏Effective Java,如果您觉得看完之后对你有所帮助,欢迎订阅本专栏,也欢迎您将本专栏分享给您身边的工程师同学. 之前在<如何说服你的同事使用TDD>中介绍了为什么要 ...

  6. 在Spring 框架中如何更有效的使用JDBC?

    使用Spring JDBC 框架,资源管理以及错误处理的代价都会减轻.开发人员只需通过statements 和queries 语句从数据库中存取数据.Spring 框架中通过使用模板类能更有效的使用J ...

  7. 如何在Spring boot中修改默认端口

    文章目录 介绍 使用Property文件 在程序中指定 使用命令行参数 值生效的顺序 如何在Spring boot中修改默认端口 介绍 Spring boot为应用程序提供了很多属性的默认值.但是有时 ...

  8. Java访问静态常量_Java如何在Spring EL中访问静态方法或常量?

    在这个例子中,您将学习如何使用Spring Expression Language访问类范围的方法或常量.要访问类范围的方法或常量T(),例如,您将需要使用Spring EL的运算符T(java.la ...

  9. 在Spring框架中使用SQL存储过程

    Spring框架也支持对SQL存储过程的调用,SQL存储过程是一组预先定义好的SQL语句,并存储到数据库管理系统中,外部程序可以直接调用执行.本课主要讨论在Spring框架中应用程序如何调用MySQL ...

最新文章

  1. Future有返回值的线程
  2. Focal Loss 的Pytorch
  3. 利用计算机漏洞犯罪,利用漏洞非法谋利2000元怎么处罚
  4. iOS开发系列文章(持续更新……)
  5. Flask入门系列(转载)
  6. 听说,阿里“拆中台”了?
  7. Laravel核心解读--HTTP内核
  8. java 建立ssh隧道_如何使用IntelliJ和JDBC SSH隧道并连接到数据库?
  9. linux NTP服务
  10. tiny core linux 7.1,极度简约 最小 Linux 发行版 Tiny Core Linux 7.1 发布
  11. 软件测试中测试用例的简单案例
  12. CentOS7安装搜狗输入法
  13. 如何才能更持久系列之——如何才能准确测量APP的功耗
  14. SpringBoot入门项目——holleWorld
  15. [Android] Joystick游戏手柄开发
  16. 用html做相册需要什么,如何快速有效的生成HTML相册?
  17. Mac Redis开机自启动
  18. 在线生成各种印章式图片
  19. 安徽阜阳育英计算机学校,阜阳育英中学
  20. QQ邮箱设置企业邮箱别名邮箱

热门文章

  1. 蓝桥杯 ALGO-158 算法训练 sign函数
  2. 蓝桥杯 ALGO-105 算法训练 黑色星期五
  3. [Java] 蓝桥杯BASIC-18 基础练习 矩形面积交
  4. [Java] 蓝桥杯BASIC-21 基础练习 Sine之舞
  5. linux下mongodb的安装及启动
  6. 【持久化框架】SpringMVC+Spring4+Mybatis3 集成,开发简单Web项目+源码下载
  7. centos7下yum安装mysql
  8. Android签名打包详解
  9. reverse-nodes-in-k-group
  10. jQuery 鼠标拖拽移动窗口