Spring的SpEL的属性注入
SpEL(Spring Expression Language),即Spring表达式语言,是比JSP的EL更强大的一种表达式语言。在运行时查询和操作数据,尤其是数组列表型数据,因此可以缩减代码量,优化代码结构。
1、注入一般属性
#{SpEL}
大括号当中可以执行一些计算,调用其他类的方法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- Spring的属性注入的方式 -->
<!-- 构造方法的方式 -->
<!-- set方法方式符注入 -->
<!-- SpEL的属性注入 -->
<bean id="car2" class="com.itzheng.spring.demo4.Car2">
<property name="name" value="#{'拖拉机' }"></property>
<property name="price" value="#{30000}"></property>
</bean>
</beans>
package com.itzheng.spring.demo4;
/*
* set方法的属性注入
*/
public class Car2 {
private String name;
private Double price;
public void setName(String name) {
this.name = name;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Car2 [name=" + name + ", price=" + price + "]";
}
}
@Test
public void demo5() {
// 创建工程
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获得到对应XML当中的对象
// 获得到对应XML当中的对象
Car2 car2 = (Car2) applicationContext.getBean("car2");
System.out.println(car2);
}
2、注入对象
(1)直接注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- Spring的属性注入的方式 -->
<!-- 构造方法的方式 -->
<!-- SpEL的属性注入 -->
<bean id="car2" class="com.itzheng.spring.demo4.Car2">
<property name="name" value="#{'拖拉机' }"></property>
<property name="price" value="#{30000}"></property>
</bean>
<bean id="employee" class="com.itzheng.spring.demo4.Employee">
<property name="name" value="#{'武九' }"></property>
<property name="car2" value="#{car2}"></property>
</bean>
</beans>
package com.itzheng.spring.demo4;
public class Employee {
private String name;
private Car2 car2;
public void setName(String name) {
this.name = name;
}
public void setCar2(Car2 car2) {
this.car2 = car2;
}
@Override
public String toString() {
return "Employee [name=" + name + ", car2=" + car2 + "]";
}
}
@Test
public void demo5() {
// 创建工程
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获得到对应XML当中的对象
// 获得到对应XML当中的对象
Car2 car2 = (Car2) applicationContext.getBean("car2");
System.out.println(car2);
}
(2)通过方法获得值,注入到另外一个对象的属性
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- Spring的属性注入的方式 -->
<!-- 构造方法的方式 -->
<!-- SpEL的属性注入 -->
<bean id="carInfo" class="com.itzheng.spring.demo4.CarInfo">
</bean>
<bean id="car2" class="com.itzheng.spring.demo4.Car2">
<property name="name" value="#{carInfo.name}"></property>
<property name="price" value="#{carInfo.calculatorPrice()}"></property>
</bean>
</beans>
package com.itzheng.spring.demo4;
public class CarInfo {
private String name;
public String getName() {
return "摩托车";
}
public Double calculatorPrice() {
return Math.random() * 3000;
}
}
package com.itzheng.spring.demo4;
public class CarInfo {
private String name;
public String getName() {
return "摩托车";
}
public Double calculatorPrice() {
return Math.random() * 3000;
}
}
测试
测试