Bean的概念
- 可復(fù)用的Java軟件組件璧坟,理論上講,任何一個(gè)java類都可以是一個(gè)Bean讶隐。但通常情況下起胰,由于Bean是被容器所創(chuàng)建的,所以Bean應(yīng)該具有一個(gè)無參的構(gòu)造器整份,另外Bean還需要實(shí)現(xiàn)Serializable接口用語實(shí)現(xiàn)Bean的持久性待错。
- 實(shí)際上,它相當(dāng)于微軟COM模型中的COM組件烈评,而Enterprise Java Bean(EJB)相當(dāng)于DCOM火俄,即分布式組件。
- Bean對象需提供get/set方法來給對應(yīng)的屬性完成依賴注入(該屬性可以是對象的屬性成員讲冠,也可以是對象包含的組件的屬性)
- 用戶通過容器來創(chuàng)建瓜客、管理、訪問Bean
Bean的XML定義規(guī)則
- 必選屬性,id:表示Bean的名稱谱仪,可以通過getBean(“id")獲取Bean實(shí)例玻熙;
- 必選屬性,class:表示Bean對應(yīng)的類路徑疯攒;
- 可選屬性嗦随,Singleton:默認(rèn)為true,即單實(shí)例模式敬尺,每次getBean獲取的都是
同一個(gè)實(shí)例枚尼,如果設(shè)置為false,即原型模式砂吞,則每次獲取的是新創(chuàng)舉的實(shí)例署恍; - 可選屬性,Init-method:在Bean實(shí)例化后要調(diào)用的方法蜻直;
- 可選屬性盯质,Destroy-method:Bean從容器里刪除之前要調(diào)用的方法;
- 可選屬性概而,Autowire:其屬性通過何種方法進(jìn)行屬性的自動轉(zhuǎn)配呼巷,默認(rèn)自動裝配;
Bean的實(shí)例化
- 使用構(gòu)造器實(shí)例化(項(xiàng)目常用方案)
<bean id=“personService” class=“cn.mytest.service.impl.PersonServiceBean"/>
- 使用靜態(tài)工廠方法實(shí)例化
public class PersonServiceFactory {
public static PersonServiceBean createPersonServiceBean(){
return new PersonServiceBean();
}
}
<bean id="personService" class="cn.mytest.service.impl.PersonServiceFactory" factory-method="createPersonServiceBean"/>
- 使用工廠方法實(shí)例化
public class PersonServiceFactory {
public PersonServiceBean createPersonServiceBean() {
return new PersonServiceBean();
}
}
<bean id=“personServiceFactory” class=“cn.mytest.service.impl.PersonServiceFactory”></bean>
<bean id=“personService” factory-bean=“personServiceFactory” factory-method=“createPersonServiceBean”></bean>
Bean的依賴注入
- Setter Injection(Set方法注入)
<bean id=“exmapleBean” class = “examples.ExampleBean”>
<property name=“beanOne”>
<ref bean=“anotherExampleBean”/>
</property>
<property name=“beanTwo” ref=“yetAnotherBean”/>
<property name=“integerProperty” value =“1”/>
</bean>
- Constructor Injection(構(gòu)造函數(shù)注入)
<bean id=“exampleBean” class=“examples.ExampleBean">
<constructor-arg type=“examples.AnontherExampleBean” ref=“anotherExampleBean" >
<constructor-arg type=“int” value=“7500”/>
<constructor-arg type=“java.lang.String” value=“42”/>
</bean>
- Inner beans
<bean id=“outer” class=“examples.ExampleBean”?
<property name=“target”>
<bean class=“com.exmaple.Person”/>
</property>
</bean>
- Collections/Maps
<bean id=“foo” class=“…”>
<property name=“accounts”>
<map>
<entry key=“one” value=“9.99”/>
</map>
</property>
</bean>
- NULLS
<bean class=“examples.ExampleBean”>
<property name=“email”><null/></property>
</bean>
Bean的自動綁定(autowire)
- byName(出于研發(fā)效率到腥,目前多數(shù)使用這個(gè)朵逝。程序員之間的約定俗成)
<beans … … default-autowire=“byName”>
… ...
</beans>
- byType
<beans … … default-autowire=“byType”>
… ...
</beans>
- autodetect
Bean的scope(作用域)
- prototype:每次都會創(chuàng)建一個(gè)新的實(shí)例
- singleton:每次返回同一個(gè)實(shí)例(注意這種情況,不要在對象中用全局變量存儲過程中的數(shù)據(jù))乡范,默認(rèn)為true
Bean的生命周期
- 第一步就是屬性注入
Bean的自定義初始化
init-method屬性
Bean的自定義銷毀
destroy-method屬性
Bean的依賴
depens-on屬性配名,描述當(dāng)前Bean必須等待其他Bean被成功加載后才能加載
Bean的Lazily-instantiated(延遲初始化)
lazy-init屬性,描述當(dāng)前Bean只有在被其他Bean引用到時(shí)晋辆,才真正創(chuàng)建實(shí)例
Bean定義的繼承
類似于類的繼承渠脉,Bean的定義可以繼承,子Bean自動繼承父Bean的所有屬性配置瓶佳,并可以覆蓋父Bean的屬性配置芋膘。
Bean的繼承
如果多個(gè)Bean都是一個(gè)類的實(shí)例,如配置多個(gè)數(shù)據(jù)源時(shí)霸饲,大部分配置的屬性都一樣为朋,只有少部分不一樣,經(jīng)常是copy上一個(gè)的定義厚脉,然后修改不一樣的地方习寸。其實(shí)Bean定義也可以像類一樣進(jìn)行繼承。
<bean id="testBeanParent" abstract="true" class="com.wanzheng90.bean.TestBean">
<property name="param1" value="父參數(shù)1"/>
<property name="param2" value="父參數(shù)2"/>
</bean>
<bean id="testBeanChild1" parent="testBeanParent"/>
<bean id="testBeanChild2" parent="testBeanParent">
<property name="param1" value="子參數(shù)1"/>
</bean>
- testBeanParent是父Bean傻工,其中abstract=“true”表示testBeanParent是抽象Bean霞溪,不會被創(chuàng)建孵滞,類似于抽象類
- testBeanChild1和testBeanChild2繼承了testBeanParent
- testBeanChild2重現(xiàn)對屬性param1進(jìn)行了配置,因此覆蓋了testBeanParent對param1屬性的配置