Spring入门第十课
Spring表达式语言:SpEL
Spring表达式语言(简称SpEL)是一个支持运行时查询和操作对象图的强大的表达式语言。
语法类似于EL:SpEL使用#{...}作为定界符,所有在大括号中的字符都将被认为是SpEL
SpEL为bean的属性进行动态复制提供了便利。
通过SpEL可以实现:
-通过bean的id对bean进行引用
-调用方法以及引用对象中的属性
-计算表达式的值
-正则表达式的匹配
下面看如何使用
package logan.spring.study.spel;
public class Car {
private String brand;
private int price;
private double tyrePerimeter;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public double getTyrePerimeter() {
return tyrePerimeter;
}
public void setTyrePerimeter(double tyrePerimeter) {
this.tyrePerimeter = tyrePerimeter;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter=" + tyrePerimeter + "]";
}
}
package logan.spring.study.spel;
public class Address {
private String city;
private String street;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + "]";
}
}
package logan.spring.study.spel;
public class Person {
private String name;
private Car car;
//引用 address bean 的city属性
private String city;
//根据car的price确定info:car的price >=300000位金领,否则为白领
private String info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Person [name=" + name + ", car=" + car + ", city=" + city + ", info=" + info + "]";
}
}
配置文件
<?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="address" class="logan.spring.study.spel.Address">
<!-- 使用spel为属性赋一个字面值 -->
<property name="city" value="#{'BeiJing'}"></property>
<property name="street" value="WuDaoKou"></property>
</bean> <bean id="car" class="logan.spring.study.spel.Car">
<property name="brand" value="Audi"></property>
<property name="price" value="500000"></property>
<!-- 使用SPEL使用类的静态属性 -->
<property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"></property>
</bean> <bean id="person" class="logan.spring.study.spel.Person">
<property name="car" value="#{car}"></property>
<property name="city" value="#{address.city}"></property>
<property name="info" value="#{car.price > 30 ? '金领':'白领'}"></property>
<property name="name" value="Tom"></property>
</bean> </beans>
package logan.spring.study.spel; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-spel.xml");
Address address = (Address) ctx.getBean("address");
System.out.println(address); Car car = (Car) ctx.getBean("car"); System.out.println(car); Person person = (Person) ctx.getBean("person");
System.out.println(person); } }
下面是输出结果
五月 21, 2017 10:01:50 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7aec35a: startup date [Sun May 21 10:01:50 CST 2017]; root of context hierarchy
五月 21, 2017 10:01:50 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-spel.xml]
Address [city=BeiJing, street=WuDaoKou]
Car [brand=Audi, price=500000, tyrePerimeter=251.32741228718345]
Person [name=Tom, car=Car [brand=Audi, price=500000, tyrePerimeter=251.32741228718345], city=BeiJing, info=金领]
Spring入门第十课的更多相关文章
- Spring入门第十九课
后置通知 看代码: package logan.study.aop.impl; public interface ArithmeticCalculator { int add(int i, int j ...
- Spring入门第十八课
Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或者基于XML配置的AOP 看代码: package lo ...
- Spring入门第十六课
接上一次讲课 先看代码: package logan.spring.study.annotation.repository; public interface UserRepository { voi ...
- Spring入门第十五课
泛型依赖注入 看代码: package logan.spring.study.generic.di; public class BaseRepository<T> { } package ...
- Spring入门第十四课
基于注解的方式配置bean(基于注解配置Bean,基于注解来装配Bean的属性) 在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpath ...
- Spring入门第十二课
Bean的配置方法 通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean 通过调用静态工厂方法创建Bean 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户 ...
- Spring入门第六课
XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean.需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式 ByType(根据类型自动装配):若I ...
- Spring入门第五课
集合属性 在Spring中可以通过一组内置的xml标签(如:<list>,<set>,<map>)来配置集合属性. 配置java.util.List类型的属性,需要 ...
- Spring入门第四课
注入参数详解:null值和级联属性 可以使用专用的<null/>元素标签为Bean的字符串或其他对象类型的属性注入null值. 和Struts,Hiberante等框架一样,Spring支 ...
随机推荐
- springboot @ConfigurationProperties @EnableConfigurationProperties @Bean @ Component
https://www.cnblogs.com/duanxz/p/4520571.html https://juejin.im/post/5cbeaa26e51d45789024d7e2 1. Bea ...
- centos7 安装jdk9 总结
升级jdk, 从jdk8 升级到jdk9 1:卸载jdk8: 1〉 [root@localhost conf.d]# rpm -qa|grep java javapackages-tools-3.4. ...
- c#中多线程写DataGridView出现滚动栏导致程序卡死(无响应)的解决的方法
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013529927/article/details/24225567 由于写的程序涉及到多线程维护一 ...
- session,cookie的理解(总结)
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 《程序员代码面试指南》第八章 数组和矩阵问题 找到无序数组中最小的k 个数
题目 找到无序数组中最小的k 个数 java代码 package com.lizhouwei.chapter8; /** * @Description: 找到无序数组中最小的k 个数 * @Autho ...
- Android Weekly Notes Issue #261
Android Weekly Issue #261 June 11th, 2017 Android Weekly Issue #261 本期内容包括: Adaptive Icons; Kotlin实现 ...
- ubuntu 网络配置及ssh文件传输
一.ubuntu网路配置 参考http://www.cnblogs.com/rusty/archive/2011/04/06/2007139.html /etc/network/interfaces ...
- CodeForces - 540B School Marks —— 贪心
题目链接:https://vjudge.net/contest/226823#problem/B Little Vova studies programming in an elite school. ...
- entity framework WithRequiredDependent和WithRequiredPrincipal
A->WithRequiredDependent->B 表示 A中包含B的不为null实例 ,A是主键实体 B是外键实体 A->WithRequiredPrincipal-> ...