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

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