別名,import,注入
Bean別名的配置
<bean id="user5" class="com.woshi.pojo.User" name="U5,User5,yonghu5">
<constructor-arg name="name" value="吳七"></constructor-arg>
</bean>
name 是別名,也可以通過getBean調用,與alias的作用相同
<alias name="user5" alias="Yonghu5"></alias>
import:引入其他配置文件
<import resource="beans.xml"></import>
<import resource="beans1.xml"></import>
<import resource="beans2.xml"></import>
<import resource="beans3.xml"></import>
<import resource="beans4.xml"></import>
將多個xml文件導入到一個里面(作為總配置文件),這樣就可以通過ClassPathXmlApplicationContext來一統調用(雖然ClassPathXmlApplicationContext能夠讀取多個xml——d=====( ̄▽ ̄*)b
依賴注入(DI)
依賴注入有
基於構造器的注入
<beans>
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg ref="beanTwo"/>
<constructor-arg ref="beanThree"/>
</bean>
<bean id="beanTwo" class="x.y.ThingTwo"/>
<bean id="beanThree" class="x.y.ThingThree"/>
</beans>
假設ThingTwo和ThingThree類沒有繼承關系,則不存在潛在的歧義(因為例子中沒有指定成員變量,屬於靠類型來匹配的情況,如果ThingTwo和ThingThree是繼承關系,可能會發生類型轉換而導致注入錯誤卻不報錯)。
基於構造器的注入其實是通過調用對應對象相應的構造器實現的。調用一個靜態的工廠方法(帶有構造bean的特定參數)也是差不多的 。
構造器注入適用於創建不需要經常變動的對象,因為構造器注入的組件始終以完全初始化的狀態返回到調用處的代碼,並且能保證所需要的依賴項不為null。
關於基於構造器配置對象的一些使用:https://www.cnblogs.com/woshi123/p/12436784.html
-
拓展:c命名空間的注入
<bean id="user2" class="com.woshi.entity.User" c:age="18" c:name="李四"></bean>c: 相當於constructor-arg ,是基於構造器的注入
set方式注入
Student對象
public class Student {
private String name;
private Integer age;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,Long> card;
private Set<String> games;
private Student girlfriend;
private Properties info;
//...各個成員變量的set方法
}
對應的bean對象裝配
<bean id="address" class="com.woshi.entity.Address">
<property name="address" value="中國"></property>
</bean>
<bean id="girlstudent" class="com.woshi.entity.Student"></bean>
<bean id="student" class="com.woshi.entity.Student">
<!--依賴基本數據包裝類注入-->
<property name="name" value="張三"></property>
<!--注入null-->
<property name="age">
<null/>
</property>
<!--依賴對象注入-->
<property name="address" ref="address"></property>
<!--依賴數組注入-->
<property name="books">
<array>
<value>紅樓夢</value>
<value>三國演義</value>
<value>水滸傳</value>
<value>西游記</value>
</array>
</property>
<!--依賴List注入-->
<property name="hobbies">
<list>
<value>打籃球</value>
<value>玩游戲</value>
</list>
</property>
<!--依賴Map注入-->
<property name="card">
<map>
<entry key="身份證" value="12309812039238457"></entry>
<entry key="學生證" value="8888888888888"></entry>
</map>
</property>
<!--依賴Set注入-->
<property name="games">
<set>
<value>lol</value>
<value>gal</value>
</set>
</property>
<!--依賴對象注入-->
<property name="girlfriend" ref="girlstudent"></property>
<!--依賴property注入-->
<property name="info">
<props>
<prop key="id">0129384</prop>
<prop key="name">張三</prop>
<prop key="username">stuZhang</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
set注入主要應用於一些類中的可選擇依賴項,也就是允許了,當對象初始化完成時,存在着一些依賴項為null。
set注入的兩一個好處時,可使類的對象在以后重新配置或注入。
set還可以配置循環注入,即A依賴於B,B依賴於A,構造器配置則不能實現這種,會引發:BeanCurrentlyInCreationException。
-
拓展:p命名空間注入
<bean id="user" class="com.woshi.entity.User" p:age="18" p:name="張三"></bean>p: 的作用就相當於property ,是基於set方法和無參構造器的注入
depends-on注入(嚴格來講不算是注入)
```xml
<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
<property name="manager" ref="manager" />
</bean>
<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />
```
一個bean是另一個bean的依賴項的話,通常可以使用ref元素或標簽來完成依賴注入。
但是,有時bean之間的依賴不太直接,該depends-on屬性可以在初始化使用此元素的bean之前顯式強制初始化一個或多個bean。
depends-on會影響“關機順序”:在bean被銷毀時,depends-on指定的bean,會先被銷毀。
idref:把配置文件中的bean的id當作字符串,而不當作ref引用
<bean id="theTargetBean" class="..."/>
<bean id="theClientBean" class="...">
<property name="targetName">
<idref bean="theTargetBean"/>
</property>
</bean>
