为了更加灵活,Spring 还提供了表达式语言Spring EL 。通过Spring EL 可以拥有更为强大的运算规则来更好地装配Bean。
SpringEL表达式
Spring表达式语言(简称SpEL)是一个支持查询和操作运行时对象导航图功能的强大的表达式语言。不直接依赖于Spring,可独立使用。底层实现:接口ExpressionParser
负责解析表达式字符串。
XML中使用
相应的实体定义
package com.rrc.entity;
import lombok.Data;
@Data
public class Customer {
private String name;
private String telephone;
}
package com.rrc.config;
import com.rrc.entity.Customer;
import lombok.Getter;
import lombok.Setter;
public class ConsumerComponent {
@Getter
@Setter
private Customer customer;
@Getter
@Setter
private String custName;
@Getter
@Setter
private String telephone;
}
XML配置,在resources目录下新建spring-other.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="customer" class="com.rrc.entity.Customer">
<property name="name" value="张三"/>
<property name="telephone" value="13666666666"/>
</bean>
<bean id="customerDao" class="com.rrc.config.ConsumerComponent">
<property name="customer" value="#{customer}"></property>
<property name="custName" value="#{customer.name}"></property>
<property name="telephone" value="#{customer.telephone}"></property>
</bean>
</beans>
我们在Application中打印ConsumerComponent类下三个属性的值
package com.rrc;
import com.rrc.config.ConsumerComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ImportResource;
@Slf4j
@SpringBootApplication
@ImportResource(locations = "classpath:spring-other.xml")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
ApplicationContext ctx
= new AnnotationConfigApplicationContext(Application.class);
ConsumerComponent consumerComponent = ctx.getBean(ConsumerComponent.class);
log.info("consumerComponent custName {}", consumerComponent.getCustName());
log.info("consumerComponent telephone {}", consumerComponent.getTelephone());
log.info("consumerComponent customer {}", consumerComponent.getCustomer());
}
}
2021-07-29 01:02:09.045 [main] INFO com.rrc.Application 21 main - consumerComponent custName 张三
2021-07-29 01:02:09.047 [main] INFO com.rrc.Application 22 main - consumerComponent telephone 13666666666
2021-07-29 01:02:09.047 [main] INFO com.rrc.Application 23 main - consumerComponent customer Customer(name=张三, telephone=13666666666)
JAVA中使用
我们把在XML中的customerDao的配置从XML移动到JAVA文件中,相应的修改如下。启动项目之后依旧可以打印出相关的信息。
package com.rrc.config;
import com.rrc.entity.Customer;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Data
public class ConsumerComponent {
@Value("#{customer}")
private Customer customer;
@Value("#{customer.name}")
private String custName;
@Value("#{customer.telephone}")
private String telephone;
}
EL使用场景
方法:EL可以调用另一个对象的方法或者属性
@Value("#{originBean.getELvalue()}")
private String telephone;
package com.rrc.config;
import org.springframework.stereotype.Component;
@Component
public class OriginBean {
public String getELvalue() {
return "EL METHOD";
}
}
构造:EL可以调用new关键字,实现构造方法调用,实例化出对象来
@Value(“#{new int[]{1,2,3}}”)
@Value(“#{new 包名.类名()}”)
package com.rrc.config;
import com.rrc.entity.School;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Data
public class ConsumerComponent {
@Value("#{originBean.getELvalue()}")
private String telephone;
@Value("#{new int[]{1, 2, 3}}")
private int[] intArray;
@Value("#{new com.rrc.entity.School()}")
private School school;
}
操作符
- EL支持大多数的算数运算符,
@Value("#{3+4}")
- 关系运算符
@Value("#{1^1}")
- 逻辑运算符
@Value("#{5>3}")
- 三元运算符
@Value("#{1> 2? 0:1}")
引用: