最近我一直在考慮一個問題,華為的BME架構與裸奔的Struts2整合Spring到底差別在哪裡呢?由於多次直接使用BME架構來開發MTV Portal系列項目,覺得BME架構整合整合Spring和Struts2做得相當好,使用比較方便,就想徹底弄明白BME開發架構和直接使用Struts2+Spring究竟有哪些不同?當然要搞清楚這個問題必須有3個前提:第一個是明白Struts2整合Spring的使用方式;第二個是明白Struts2的啟動和載入設定檔的流程;第三個是如何控制Spring載入設定檔流程和建立業務對象的流程。第一個需要具體實踐,第二個和第三個先決條件需要研讀Struts2和Spring的原始碼。
BME架構是一個綜合的架構,WebUI只是其中的一部分,它的WebUI部分是在Struts2+Spring2.0+Spring WebFlow的基礎上構建的。本篇的後續部分將BME架構的WebUI模組簡稱為UI,當然UI比當前市場上流傳出來的Struts2+Spring2.0架構使用起來要方便一些,我這裡主要分析的就是UI與直接使用Struts2+Spring2.0的差別以及UI的這個優點我們是如何?的?
UI與Struts2整合Spring2.0的最大區別在於UI使用的是統一的設定檔,將Struts2的設定檔與Spring2.0的設定檔無縫的結合在一起了。
在使用Struts2整合Spring2.0的時候典型的Spring的設定檔applicationContext.xml格式如下:<?xml version="1.0" encoding="UTF-8"?><br /><beans<br />xmlns="http://www.springframework.org/schema/beans"<br />xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><br /> <bean id="loginService" class="com.ssh2.web.login.service.LoginServiceImpl"/><br /> <bean id="loginAction" class="com.ssh2.web.login.LoginAction"><br /> <property name="loginService" ref="loginService" ></property><br /> </bean><br /></beans>
在使用Struts2整合Spring2.0的時候典型的Struts2的設定檔struts.xml格式如下:<?xml version="1.0" encoding="UTF-8" ?><br /><!DOCTYPE struts PUBLIC<br /> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"<br /> "http://struts.apache.org/dtds/struts-2.0.dtd"><br /><struts><br /> <constant name="struts.enable.DynamicMethodInvocation" value="false" /><br /> <constant name="struts.devMode" value="false" /><br /> <constant name= "struts.objectFactory" value="spring" /><br /> <package name="ssh2" namespace="/ssh2" extends="struts-default"><br /><action name="login" class="loginAction" method="login"><br /> <result name="success">/login.jsp</result><br /> <result name="error">/login.jsp</result><br /></action><br /><action name="doLogin" class="loginAction" method="doLogin"><br /> <result name="success">/main.jsp</result><br /> <result name="error">/error.jsp</result><br /></action><br /> </package><br /></struts>
這裡的設定檔有一個問題,就是對於Action存在重複的配置,在Spring中要配置一次,在Struts2中也要配置一次,同樣一個Action類要遵循Struts2的規範和遵循Spring2.0的規範配置了兩次,讓人從感覺上來說就脫離開來了,結合得不那麼完美。對於這個問題BME架構的UI模組就解決得非常的美妙了,它仿照並且擴充Spring2.0和Struts2的設定檔,對於Action再也不需要在兩邊都配置了,只需要在Action的設定檔中一次了。
上述例子在BME的UI架構中則可以如下配置。
對於使用Spring的業務層,典型的設定檔login.service.xml檔案格式如下:<bme:bean id="loginService" class="com.ssh2.web.login.service.LoginServiceImpl"/>
對於使用Struts2的WEB層,典型的設定檔login.web.xml檔案格式如下:<bme:package name="ssh2" namespace="/ssh2" extends="struts-default"><br /><bme:action name="login" class="com.ssh2.web.login.LoginAction" method="login"><br /><bme:property name="loginService" ref="loginService" /><br /><bme:result name="success">/login.jsp</result><br /><bme:result name="error">/login.jsp</result><br /></bme:action><br /><bme:action name="doLogin" class="com.ssh2.web.login.LoginAction" method="doLogin"><br /><bme:property name="loginService" ref="loginService" /><br /><bme:result name="success">/main.jsp</result><br /><bme:result name="error">/error.jsp</result><br /></bme:action><br /></bme:package>
結合得多麼完美啊!
那麼這一切又是如何?的呢?