之前學習basic的時候有個疑問就是不知道如何實現bean中引用其他的bean的屬性,當時是用ref來實現對其他bean的引用,但是ref必需引用的是一個常量。所以這種方式來實現對其他bean中的屬性的引用是不合理的。
當我看到Spring Expression Language時發現原來我想實現的'ref'的效果就是使用Spring EL這種表達式就可以完成了。
?
而Spring EL有兩種配置形式,一種是Spring EL 寫在xml中,和zai xml定義bean一樣,通過在定義bean的xml標簽內使用"#{otherBean.property}" 或#{otherBean}的形式來完成。另一種是基于注解的方式。一下稍作說明:
第一種和第二種都是通過xml來加載spring context的但是xml里的內容不一樣
第一種:直接在xml中定義spring 的上下文,并在bean中的屬性字段直接使用SpringEL來引用其他bean或其他bean的屬性
第二種: 在xml中定義Component的scan-package的包名,這樣spring會在這個包中掃描并配置對應的bean,然后我們在對應的類中加上注解就可以了.
?
第一種:
<?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="itemBean" class="com.ric.demo.Item">
<property name="name" value="itemA"/>
<property name="qty" value="10"/>
</bean>
<bean id="customerBean"
class="com.ric.demo.Customer">
<property name="item" value="#{itemBean}" />// 直接在這里使用Spring EL設置值
<property name="itemName" value="#{itemBean.name}"/>// 直接在這里使用Spring EL設置值
</bean>
</beans>
第二種
定義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.ric.demo" />
</beans>
配置對應的bean
@Component("customerBean")
public class Customer {
private Item item;
private String itemName;
@Override
public String toString() {
return "Customer{" +
"item=" + item +
", itemName='" + itemName + '\'' +
'}';
}
public Item getItem() {
return item;
}
// 使用Spring EL 來取到對應的value
@Value("#{itemBean}")
public void setItem(Item item) {
this.item = item;
}
public String getItemName() {
return itemName;
}
// 使用Spring EL 來取到對應的value
@Value("#{itemBean.name}")
public void setItemName(String itemName) {
this.itemName = itemName;
}
}
?需要注意的是如果這里的 Spring EL里面引用的是其他的對象有一下這種效果
?
ApplicationContext context = new ClassPathXmlApplicationContext("BeansAutoScan.xml");
Item item = (Item)context.getBean("itemBean");
System.out.println(item);
Customer customer = (Customer)context.getBean("customerBean");
System.out.println(customer);
item.setName("geek");
System.out.println(item);
System.out.println(customer);
Item{name='item', qty=10}
Customer{item=Item{name='item', qty=10}, itemName='item'}
Item{name='geek', qty=10}
Customer{item=Item{name='geek', qty=10}, itemName='item'}
Process finished with exit code 0
也就是說如果引用的是item bean 我們的customer的itemBean屬性的確是對item這個bean的一個引用。
但是Customer的itemName這個屬性的值沒有變,因為bean掃描的時候也是為上下文中的bean做一個初始化的工作,初始化完后item雖然對name做了一個更新的操作,但是customer的itemName的指針不會因為item的name做了變化而同步,而是依舊指向item原先name值的那個指針
?
?
?
?
?
?
?
?
?
?
?
?
?
? ? ? ??