?注:在JavaEye注冊(cè)不久,前些天這篇文章發(fā)布在了blog首頁(yè),似乎大家都在論壇活動(dòng)多,因此轉(zhuǎn)移到論壇這邊分享
關(guān)鍵字: spring 動(dòng)態(tài)bean管理 零配置
1. Spring業(yè)務(wù)bean配置的困擾
??? 利用Spring IoC 容器進(jìn)行項(xiàng)目開(kāi)發(fā)的一個(gè)重要工作就是在 applicationContext.xml 中進(jìn)行bean管理。然而,業(yè)務(wù)bean(BO或Service),DAO bean的配置量隨項(xiàng)目的功能規(guī)模增長(zhǎng)而增長(zhǎng),對(duì)于大型項(xiàng)目而言,龐大的 bean配置文件維護(hù)起來(lái)極為痛苦。例如:
- <!--?業(yè)務(wù)對(duì)象?--> ????? ??
- < bean ? id = "authService" ? parent = "baseTransactionProxy" > ????? ??
- ???? < property ? name = "target" > ????? ??
- ???????? < bean ? class = "com.sr.jsfuser.service.AuthorityService" > ????? ??
- ???????????? < property ? name = "authDao" ? ref = "authDao" ? /> ????? ??
- ???????? </ bean > ????? ??
- ???? </ property > ????? ??
- </ bean > ????? ??
- ????? ??
- < bean ? id = "userService" ? parent = "baseTransactionProxy" > ????? ??
- ???? < property ? name = "target" > ?????????? ??
- ???????? < bean ? class = "com.sr.jsfuser.service.pojo.UserService" > ????? ??
- ???????????? < property ? name = "userDao" ? ref = "userDao" ? /> ????? ??
- ???????? </ bean > ????? ??
- ???? </ property > ????? ??
- </ bean > ????? ??
- <!--?數(shù)據(jù)訪問(wèn)對(duì)象?--> ????? ??
- < bean ? id = "authDao" ? class = "com.sr.jsfuser.dao.jdbc.AuthorityDao" > ????? ??
- ???? < property ? name = "dataSource" ? ref = "dataSource" ? /> ????? ??
- </ bean > ????? ??
- ????? ??
- < bean ? id = "userDao" ? class = "com.sr.jsfuser.dao.jdbc.UserDao" > ????? ??
- ???? < property ? name = "dataSource" ? ref = "dataSource" ? /> ????? ??
- </ bean > ??????
上例為兩個(gè)功能的配置,鑒權(quán)(auth)和用戶(hù)(user)。這種Service bean和DAO bean 的管理方式是按功能的管理方式,即one by one,配置量是
???????? F=f(n)
???
?? 其中 n 是項(xiàng)目中功能的數(shù)量。上例雖充分體現(xiàn)了Spring IoC容器依賴(lài)注入的優(yōu)良特性,但配置量過(guò)大,維護(hù)和管理難度很大,不是一種最佳實(shí)踐。
2. 零配置的動(dòng)態(tài)Service Bean管理(ServiceFactory) ??
???? 有沒(méi)有一種既能發(fā)揮Spring優(yōu)勢(shì),又能節(jié)省配置的模式呢?我們可以觀察到,每個(gè)功能的Service bean的配置,只有 dao 注入是不同的,其他如Service類(lèi)的命名,Service的事務(wù)管理器注入,Service類(lèi)和DAO類(lèi)的命名關(guān)系等都具有相似性或者完全相同;dao bean配置類(lèi)似。
??? 因此,可以采用動(dòng)態(tài)bean管理(Dynamic Bean Management)思想簡(jiǎn)化業(yè)務(wù)bean配置。動(dòng)態(tài)bean管理使用新定義的 ServiceFactory 的createService方法來(lái)創(chuàng)建業(yè)務(wù)類(lèi)Service實(shí)例, ServiceFactory? 在創(chuàng)建時(shí),自動(dòng)進(jìn)行dao創(chuàng)建,datasource注入等,不再需要進(jìn)行bean的配置。原理如下圖所示。
ServiceFactory原理有以下要點(diǎn):
1) 利用了Spring ApplicationContext 的 registerBeanDefinition方法和動(dòng)態(tài)bean注冊(cè)技術(shù);
2) 作為bean管理的一個(gè)規(guī)范:要求Service類(lèi)實(shí)現(xiàn) setDAO(DAO dao)方法,以便統(tǒng)一注入dao實(shí)例;
3) 作為bean管理的一個(gè)規(guī)范:調(diào)用 dao.setDatasource(datasource)方法進(jìn)行數(shù)據(jù)源注入;
4) 作為bean管理的一個(gè)規(guī)范:內(nèi)部使用className作為 service bean 和dao bean的內(nèi)部id;
5) 作為bean管理的一個(gè)規(guī)范:ServiceFactory內(nèi)獲取bean實(shí)例時(shí),都采用prototype scope
。
ServiceFactory內(nèi)部實(shí)現(xiàn)序列圖如下:
使用ServiceFactory 進(jìn)行業(yè)務(wù)開(kāi)發(fā),代碼如下:
至于事務(wù)管理,即可使用向service注入transactionManager; 也可以使用 Spring 的
??? org.springframework.transaction.interceptor.TransactionInterceptor ,
??? org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator,
??? org.springframework.aop.support.NameMatchMethodPointcutAdvisor
???? 三者配合,將 *Service類(lèi)的 特定方法納入事務(wù)范圍。我們項(xiàng)目中使用的是后者,更為簡(jiǎn)便和松耦.
3. 通用的動(dòng)態(tài)Bean管理(自定義BeanFactory類(lèi)) ??
?
?? 在上例中,注意新增了一個(gè)BeanFactory,區(qū)別于Spring 的 BeanFactory 接口。自定義 BeanFactory 通過(guò) createBean(className) 進(jìn)行動(dòng)態(tài)bean注冊(cè)和創(chuàng)建,
????????
??? 相當(dāng)于替代了Object bean = new UserService();
4. JDK問(wèn)題
???? 以上方案主要針對(duì)1.4。 但同樣適用1.5及以上。有朋友說(shuō),1.5中用 annotation + <context:component-scan/>標(biāo)簽也能完成類(lèi)似的工作,但那樣需要在 Java 代碼中寫(xiě)很多 annotation , 個(gè)人不偏好使用 annotation ,因?yàn)樗喈?dāng)于xml配置移到了 Java代碼中,并且維護(hù)不方便。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
