首页 > 编程知识 正文

spel表达式解析,spel表达式的用法

时间:2023-05-03 17:54:19 阅读:183268 作者:3965

文章目录 SpEL表达式SpEL表达式概述1、什么是SpEL表达式2、SpEL表达式的作用 SpEL的使用方式1、xml配置的方式2、注解的方式 SpEL表达式的调用

SpEL表达式 SpEL表达式概述 1、什么是SpEL表达式 SpEL:(spring expression language) 是一种表达式语言,是一种强大,简洁的装配Bean的方式。他可以通过运行期间执行的表达式将值装配到我们的属性或构造函数当中,也可以调用JDK中提供的静态常量,获取外部Properties文件中的的配置。 2、SpEL表达式的作用 能够更加简单,多元的装配Bean,弥补了XML静态注入的不足。简化开发,减少了一些逻辑、配置的编写,使代码更加简洁。

SpEL表达式的格式:#{表达式}

SpEL的使用方式 1、xml配置的方式

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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="MyMessage" class="com.jp.MyMessage"> <property name="message" value="#{systemProperties['user.language']}"></property></bean></beans>

代码

package com.jp;public class MyMessage { private String message; public String getMessage() {return message;} public void setMessage(String message) {this.message = message;}}

测试

package com.jp;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");MyMessage myMessage =context.getBean(MyMessage.class);System.out.println(myMessage.getMessage());}}

结果

解释:这里使用了表达式#{systemProperties['user.language']}来设置值,用来检索用户语言系统的属性。

2、注解的方式

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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.jp"></context:component-scan></beans>

代码

package com.jp;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class MyMessage2 {@Value("#{systemProperties['user.language']}")private String message; public String getMessage() {return message;}}

解释:这里使用了@Value注解的方式,当实例化MyMessage2这个bean的时候,将使用该注解设置默认值。此处还是使用了之前的SpEL表达式,来设置用户语言系统的属性。(在这里@Value注解既可以在类的字段属性上面,也可以在构造函数和方法上面使用)。
测试

package com.jp;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");MyMessage2 myMessage =context.getBean(MyMessage2.class);System.out.println(myMessage.getMessage());}}

结果

SpEL表达式的调用

Peron类

package com.jp;public class Person {private String name;private Integer age;private Student s;public Person() {super();}public Person(String name, Integer age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Student getS() {return s;}public void setS(Student s) {this.s = s;}}

Student类

package com.jp;public class Student {private String name;private Integer age;public Student() {super();}public Student(String name, Integer age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}

配置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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="p" class="com.jp.Person"><!-- value="xx" 等价于 value="#{stu.name}" value="stu.age" 等价于 value="#{stu.getAge()}"ref="stu" 等价于 value="#{stu}"--><property name="name" value="#{stu.name}"></property><property name="age" value="#{stu.getAge()>18?19:17}"></property><property name="s" value="#{stu}"></property></bean><bean id="stu" class="com.jp.Student"><property name="name" value="大白"></property><property name="age" value="18"></property></bean></beans>

测试类

package com.jp;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Demo {@Testpublic void testSpEL(){ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");Person p = (Person)ac.getBean("p");System.out.println(p.getName()+","+p.getAge()+","+p.getS());}}

结果

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。