日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Spring架構增強MultiActionController(下)

系統 1945 0
5.實現MultiMethodControllerUrlHandlerMapping

??? 我們在上面討論過了怎么實現MultiMethodControllerUrlHandlerMapping,要實現為具體的代碼,我們可以通過擴展org.springframework.web.servlet.handler.AbstractUrlHandlerMapping。AbstractUrlHandlerMapping擴展了org.springframework.web.context.support.WebApplicationObjectSupport。WebApplicationObjectSupport可以獲得當前WebApplicationContext。

??? 1. 重寫initApplicationContext方法,在context中查找所有MultiActionController類型的bean,把MultiActionController的urlMethodmappings屬性的key值為key值,MultiActionController實例為鍵值的鍵值對添加到一個urlMap中。?
public class MultiMethodControllerUrlHandlerMapping extends AbstractUrlHandlerMapping ... {
???
private ? Map urlMap = new HashMap();
???
public void initApplicationContext() throws BeansException ... {
??????? initialUrlMap();
??? }

???
protected void initialUrlMap()throws BeansException ... {
???????
// 找查所有MultiMethodController類型和子類型的bean到一個map中,bean Name為key值 ,bean實例為value值
??????? Map matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
??????????????? getWebApplicationContext(),
??????????????? MultiMethodController.
class , true , false );
??????? List controllers
= null ;
???????
if ( ! matchingBeans.isEmpty()) ... {
??????????? controllers
= new ArrayList(matchingBeans.values());
???????????
for ( int i = 0 ; controllers != null && i < controllers.size();i ++ ) ... {
??????????????? MultiMethodController controller
= (MultiMethodController)controllers. get (i);
??????????????? Properties urlPros
= controller.getUrlMethodmappings();
??????????????? Iterator itr
= urlPros.keySet().iterator();
???????????????
for (;itr.hasNext();) ... {
??????????????????? String url
= (String)itr.next();
??????????????????? urlMap.put(url,controller);
??????????????? }

??????????? }

??????? }

}

2. 遍歷urlMap,調用AbstractUrlHandlerMapping的registerHandler(String urlPath, Object handler)方法,依次將url與對應的handler注冊到AbstractUrlHandlerMapping的handlerMap中。

protected void registerUrlMap()throws BeansException ... {
???????
if ( this .urlMap.isEmpty()) ... {
??????????? logger.info(
" Neither 'urlMap' nor 'mappings' set on MultiMethodControllerUrlHandlerMapping " );
??????? }

???????
else ... {
??????????? Iterator itr
= this .urlMap.keySet().iterator();
???????????
while (itr.hasNext()) ... {
??????????????? String url
= (String) itr.next();
??????????????? Object handler
= this .urlMap. get (url);
???????????????
// prepend with slash if it's not present
??????????????? if ( ! url.startsWith( " / " )) ... {
??????????????????? url
= " / " + url;
??????????????? }

???????????????
// 父類方法
??????????????? registerHandler(url, handler);
??????????? }

??????? }


??? }

?然后在initApplicationContext方法中調用registerUrlMap方法
public void initApplicationContext() throws BeansException ... {
??????? initialUrlMap();
??????? registerUrlMap();
??? }

?3. 使用MultiMethodControllerUrlHandlerMapping
??? 使用MultiMethodControllerUrlHandlerMapping,只需要在ApplicationContext中,定義成一個bean就可以了。
id = " multiMethodControllerUrlHandlerMapping "
?
class = " com.prs.application.ehld.web.handler.MultiMethodControllerUrlHandlerMapping " >
?
< property name = " order " >
????
< value > 3 </ value >
?
</ property >
</ bean >
注意:在一個context如果定義多個HandlerMapping,需要為每一個HandlerMapping指定order屬性。

??? 你只需要在在context 中定義MultiMethodControllerUrlHandlerMapping,在使用MultiActionController時,只需要配置urlMethodmappings屬性就可以了。當刪除或增加一個MultiActionController的bean時,無需要連帶配置任何HandlerMapping. 簡化了bean的配置。使得MultiActionControler的bean配置只關心自身的屬性配置,而無需要去關心看起來與自身無關的HandlerMapping的配置。使得整個配置更合乎人們正常的思維邏輯,減少配置的復雜性。

6.設計討論

??? 在這里我們將對以Spring為基礎進行項目架構設計進行一些討論.????
1. MultiActionController還是AbstractController與SimpleFormController組合
在使用Spring MVC時,SimpleFormController用于表單編輯和提交;而復雜的功能則通過擴展AbstractcController完成。這就是所謂的AbstractController與SimpleFormController組合。以AbstractController與SimpleFormController的結合來完成表示層邏輯。

??? Spring MVC雖然也提供了MultiActionController,但是它似乎天生就有點蹩腳。對數據綁定支持不是很好,在用于表單編輯和提交時不像SimpleFormController那么強大。其實通過對MultiActionController的擴展和增強,完成可以實現與SimpleFormController同樣的功能,比如數據校驗等,并且還比SimpleFormController具有更多的靈活性。
在OO技術中,有一個重要的原則:低耦合,高內聚;我們應該按職責來設計對象。按對象應該具有的職責來給對象設計相應的方法。如果把一個對象本來該具有的職責分散到不同類中去完成,那么這個些類是違反“低耦合,高內聚”原則的。一個類不是高內聚的,就不便于維護和擴展,造成大量重復代碼的產生。同樣把一組相關的功能分散到多個Controller去實現,是違反“低耦合,高內聚”原則的,可以就會產生大量的重復代碼。比如參數獲取,數據校驗等。如果使用MultiActionController,把相關的功能由一個Controller的不同方法實現,集中在一個Controller類中處理,就使得這個Controller類是具有“高內聚”性的。所以,在項目應用中,相關的功能應該由一個MultiActionController的不同方法去實現。這樣就便于代碼的維護,提高代碼的重用,減少bean配置,降低項目的復雜度。

??? 2. 靈活性與簡易化???
??? Spring作為一個輕量級的j2ee基礎框架,使用是非常靈活的。特別是可以通過xml文件來靈活的配置對象之間的依賴。但是,以Spring作為框架的項目,bean的配置太多,反而增加了項目的復雜度。在開發過程中,應該把主要精力花在關注業務邏輯的實現上面,而不應該花在配置上面。靈活度越大也就導致了復雜度越高。當然,Spring是一個通用框架,應該具有這樣的靈活性,才便于擴展,以滿足各種應用需要。

??? 在具體的項目中,就應該使架構使用起來簡單,易用。特別是以Spring作為基礎的架構中,應該通過設計降低配置的復雜度,盡可能的減少bean的配置和使配置簡單化。

??? 一個bean屬性發生變化,不應該產生連帶關系,使得其它bean也需要修改配置。這是不利于團隊開發的。在團隊開發中,開發人員應該只關心業務對象的bean配置。

??? 像HandlerMapping這些屬于框架基礎bean配置一旦定義后就應該具有穩定性。不要因為業務對象bean的改變而需要開發人員隨之進行修改。

??? 3. 增強的MultiActionController與MultiMethodControllerUrlHandlerMapping
??? 通過擴展MultiActionController,使得它得到增強,能夠實現SimpleFormController的功能,同時使得配置更加直觀和簡易。
??? 只需要定義一個MultiMethodControllerUrlHandlerMapping,使得開發人員只需要關注相關MultiActionController的配置,而無需去再關注和修改HandlerMapping的配置。
??? 通過MultiMethodControllerUrlHandlerMapping 與增強的MultiActionController結合,更易于運用OO技術設計高內聚的? Controller類,減化bean的配置。讓開發人員把精力花在系統的業務邏輯的實現上,而不會去過度關心bean的配置

?

Spring架構增強MultiActionController(下)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 岳西县| 塔河县| 定陶县| 双牌县| 罗甸县| 利辛县| 永州市| 汪清县| 沂源县| 灵宝市| 保山市| 普兰县| 阿合奇县| 鹤壁市| 介休市| 铁力市| 西乌珠穆沁旗| 和政县| 堆龙德庆县| 桂东县| 屏边| 金溪县| 临洮县| 德化县| 永靖县| 依兰县| 连平县| 庆城县| 富裕县| 敖汉旗| 安庆市| 定结县| 屏边| 理塘县| 临洮县| 祁阳县| 桂阳县| 寿宁县| 诏安县| 静宁县| 广元市|