Spring的SpEL的屬性注入
SpEL:Spring Expression Language,Spring 的表達式語言。
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;
}
}
測試
測試