zl程序教程

您现在的位置是:首页 >  后端

当前栏目

springEL表达式_Spring详解

Spring 详解 表达式
2023-06-13 09:13:39 时间

大家好,又见面了,我是你们的朋友全栈君。

文章目录

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.xsd http://www.springframework.org/schema/context http://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.xsd http://www.springframework.org/schema/context http://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;
@Component
public 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.xsd http://www.springframework.org/schema/context http://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 { 

@Test
public void testSpEL(){ 

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person p = (Person)ac.getBean("p");
System.out.println(p.getName()+","+p.getAge()+","+p.getS());
}
}

结果

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/195009.html原文链接:https://javaforall.cn