说说如何在 Spring 框架中使用 SpEL 表达式

版权声明:如果喜欢,就收藏吧 O(∩_∩)O~ https://blog.csdn.net/deniro_li/article/details/82717138

在 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>    
  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哈哈~

猜你喜欢

转载自blog.csdn.net/deniro_li/article/details/82717138