华为BME框架与Struts2集成Spring的比较分析

      最近我一直在考虑一个问题,华为的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"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="loginService" class="com.ssh2.web.login.service.LoginServiceImpl"/> <bean id="loginAction" class="com.ssh2.web.login.LoginAction"> <property name="loginService" ref="loginService" ></property> </bean> </beans>

      在使用Struts2集成Spring2.0的时候典型的Struts2的配置文件struts.xml格式如下:<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name= "struts.objectFactory" value="spring" /> <package name="ssh2" namespace="/ssh2" extends="struts-default"> <action name="login" class="loginAction" method="login"> <result name="success">/login.jsp</result> <result name="error">/login.jsp</result> </action> <action name="doLogin" class="loginAction" method="doLogin"> <result name="success">/main.jsp</result> <result name="error">/error.jsp</result> </action> </package> </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"> <bme:action name="login" class="com.ssh2.web.login.LoginAction" method="login"> <bme:property name="loginService" ref="loginService" /> <bme:result name="success">/login.jsp</result> <bme:result name="error">/login.jsp</result> </bme:action> <bme:action name="doLogin" class="com.ssh2.web.login.LoginAction" method="doLogin"> <bme:property name="loginService" ref="loginService" /> <bme:result name="success">/main.jsp</result> <bme:result name="error">/error.jsp</result> </bme:action> </bme:package>  

     结合得多么完美啊!

     那么这一切又是如何实现的呢?

 

 

你可能感兴趣的:(spring,框架,struts,Class,action,华为)