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

Spring架構(gòu)增強MultiActionController(下)

系統(tǒng) 2032 0
5.實現(xiàn)MultiMethodControllerUrlHandlerMapping

??? 我們在上面討論過了怎么實現(xiàn)MultiMethodControllerUrlHandlerMapping,要實現(xiàn)為具體的代碼,我們可以通過擴展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,調(diào)用AbstractUrlHandlerMapping的registerHandler(String urlPath, Object handler)方法,依次將url與對應(yīng)的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方法中調(diào)用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配置只關(guān)心自身的屬性配置,而無需要去關(guān)心看起來與自身無關(guān)的HandlerMapping的配置。使得整個配置更合乎人們正常的思維邏輯,減少配置的復(fù)雜性。

6.設(shè)計討論

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

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

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

??? 在具體的項目中,就應(yīng)該使架構(gòu)使用起來簡單,易用。特別是以Spring作為基礎(chǔ)的架構(gòu)中,應(yīng)該通過設(shè)計降低配置的復(fù)雜度,盡可能的減少bean的配置和使配置簡單化。

??? 一個bean屬性發(fā)生變化,不應(yīng)該產(chǎn)生連帶關(guān)系,使得其它bean也需要修改配置。這是不利于團隊開發(fā)的。在團隊開發(fā)中,開發(fā)人員應(yīng)該只關(guān)心業(yè)務(wù)對象的bean配置。

??? 像HandlerMapping這些屬于框架基礎(chǔ)bean配置一旦定義后就應(yīng)該具有穩(wěn)定性。不要因為業(yè)務(wù)對象bean的改變而需要開發(fā)人員隨之進行修改。

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

?

Spring架構(gòu)增強MultiActionController(下)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 桃园县| 永兴县| 大悟县| 曲周县| 邢台市| 正阳县| 大洼县| 宁化县| 察雅县| 紫金县| 临澧县| 大田县| 广宗县| 成安县| 沾化县| 开封县| 仁寿县| 廊坊市| 方山县| 徐闻县| 当涂县| 渝北区| 洮南市| 遂溪县| 连城县| 贵定县| 苏尼特左旗| 肥西县| 年辖:市辖区| 芒康县| 木里| 筠连县| 云南省| 万年县| 邯郸市| 紫金县| 河源市| 全州县| 二连浩特市| 龙游县| 芦溪县|