Spring表达式语言

通过SpEL完成系统属性注入

新建maven项目

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
</dependencies>

在com.spring包中新建MyBean

public class MyBean {

    private String message;



    public String getMessage() {

        return message;

    }



    public void setMessage(String message) {

        this.message = message;

    }

}

在src/main/resource中新建applicationContext.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"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="myBean" class="com.spring.MyBean">

        <property name="message" value="#{systemProperties['user.language']}"/>

    </bean>

</beans>

新建main类

public class Main {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        MyBean myBean = context.getBean(MyBean.class);

        System.out.println(myBean.getMessage()  );

    }

}

示例说明

在xml配置中 message属性的值用Spring表达式语言来定义。#{systemProperties['user.language']}

systemProperties是一个系统保留字。

使用SpEL解析Hello World

在maven配置文件中添加依赖

<dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>4.11</version>

    <scope>test</scope>

</dependency>

在test/java中新建HelloWorldTest测试类

public class HelloWorldTest {

    ExpressionParser parser;

    @Before

    public void setup(){

        parser = new SpelExpressionParser();

    }

    @Test

    public void helloWorldParsedOK(){

        Expression expression = parser.parseExpression("'hello world!'");

        String value = expression.getValue(String.class);

        assertThat(value,is("hello world!"));

    }

}

运行测试类

示例说明

添加的junit依赖是为了可以运行测试类

测试类中的ExpressionParser是分析器。

在setup方法中先给ExpressionParser接口创建实例

在test方法中对字符串“hello world!“进行分析,获得Expressio实例。

再用getValue()方法来的到评估值。

最后评估该值是否相同。

通过SpEL调用Spring Bean的方法

在com.spring包中新建Show类

public class Show {

    private String instrument;

    private String song;



    public void setInstrument(String instrument) {

        this.instrument = instrument;

    }



    public void setSong(String song) {

        this.song = song;

    }

    public String guitarSong(){

        return "More Than Words";

    }

    public void present(){

        System.out.println("playing " + song + " with instrument " + instrument );

    }

}

在applicationContext.xml新建两个bean配置

<bean id="show1" class="com.spring.Show">

    <property name="song" value="Turning Tables"/>

    <property name="instrument" value="Piano" />

</bean>

<bean id="show2" class="com.spring.Show">

    <property name="instrument" value="Guitar"/>

    <property name="song" value="#{show2.guitarSong()}" />

 </bean>

在main中检测bean配置是否完成

public class Main {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        Show show1 = (Show)context.getBean("show1");

        show1.present();

        Show show2 = (Show)context.getBean("show2");

        show2.present();

    }

}

实例说明

在bean配置文件中,使用spring 表达式语言调用了bean的方法。value="#{show2.guitarSong()}"

相应的,也可以调用构造函数,调用静态方法。

发布了66 篇原创文章 · 获赞 3 · 访问量 4809

猜你喜欢

转载自blog.csdn.net/weixin_42518668/article/details/104533334