struts1.x、hibernate和spring2.x集成方式

    技术2022-05-13  4

     

    1.strutsspring集成

    Spring struts 整合的四种方式。

    1.使用Spring ActionSupport 2.使用Spring DelegatingRequestProcessor 类。3.全权委托。org.springframework.web.struts.DelegatingActionProxy

    4.使用 org.springframework.web.struts.AutowiringRequestProcessor ,只需配置controller,不需要在spring中配置。自动装载能力强

    无论用那种方法来整合第一步就是要为struts来装载spring的应用环境。就是在 struts 中加入一个插件。struts-config.xml

    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">   <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/></plug-in>

    spring 的配置文件被作为参数配置进来。这样可以省略对web.xml 文件中的配置。确保你的applicationContext.xml WEB-INF目录下面

    1,使用SpringActionSupport .Spring ActionSupport 继承至 org.apache.struts.action.Action ActionSupport的子类可以或得 WebApplicationContext类型的全局变量。通过getWebApplicationContext()可以获得这个变量。

    这是一个 servlet 的代码:public class LoginAction extends org.springframework.web.struts.ActionSupport {public ActionForward execute(ActionMapping mapping, ActionForm form,    HttpServletRequest request, HttpServletResponse response) {   LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub                //获得 WebApplicationContext 对象                WebApplicationContext ctx = this.getWebApplicationContext();     LoginDao dao = (LoginDao) ctx.getBean("loginDao");   User u = new User();     u.setName(loginForm.getName());   u.setPwd(loginForm.getPwd());       if(dao.checkLogin(u)){    return mapping.findForward("success");   }else{    return mapping.findForward("error");   }  }}

    applicationContext.xml 中的配置<beans><bean id="loginDao" class="com.cao.dao.LoginDao"/></beans>

    这中配置方式同直接在web.xml文件配置差别不大。注意:Action继承自 org.springframework.web.struts.ActionSupport 使得strutsspring耦合在一起。但实现了表示层和业务逻辑层的解耦(LoginDao dao = (LoginDao) ctx.getBean("loginDao"))

     

    通常这种方式是:抽象出一个父类,继承org.springframework.web.struts.ActionSupport,这样在action业务中不会任何其他框架的代码。

    Public class baseAction extends ActionSupport

    {

    Public Object getBean(String beanName)

    {

    Return getWebApplicationContext().getBean(beanName);

    }

    }

    想把整个策略交给springIoC特性来控制。

    Public class baseAction extends ActionSupport

    {

    Private Object serviceObj;

    Public void setServiceObj(Object serviceObj)            //这是关键的由他来实施注入策略

    {

    This.serviceObj=serviceObj;

    }

    Public Object getBean(String beanName)

    {

    Return this.serviceObj;

    }

    }

    实际上上边的参数beanName已经没有意义了。

    2,使用Spring DelegatingRequestProcessor DelegatingRequestProcessor 继承自 org.apache.struts.action.RequestProcessor 并覆盖了里面的方法。sturts-config.xml

     <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>

    通过 <controller >来替代org.apache.struts.action.RequestProcessor 的请求处理。

    public class LoginAction extends Action {//利用spring来注入这个对象。private LoginDao dao ;public void setDao(LoginDao dao) {   System.out.println("执行注入");   this.dao = dao;}

    public LoginDao getDao() {   return dao;}

    public ActionForward execute(ActionMapping mapping, ActionForm form,    HttpServletRequest request, HttpServletResponse response) {   LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub  //这样一改这行代码似乎没有必要了。   //WebApplicationContext ctx = this.getWebApplicationContext();   //LoginDao dao = (LoginDao) ctx.getBean("loginDao");   User u = new User();     u.setName(loginForm.getName());   u.setPwd(loginForm.getPwd());     //直接用dao来调用spring会将这个对象实例化。   if(dao.checkLogin(u)){    return mapping.findForward("success");   }else{    return mapping.findForward("error");   }  }}这里的。LoginAction extends Action 说明 struts 每有和spring 耦合。看一下applicationContext.xml 中的配置。<beans><bean id="loginDao" class="com.cao.dao.LoginDao"/><bean name="/login" class="com.cao.struts.action.LoginAction">   <property name="dao">    <ref local="loginDao"/>   </property></bean></beans>

    这里 name="/login" struts 中的path匹配     class="com.cao.struts.action.LoginAction" struts 中的type匹配

    还要为 LoginAction 提供必要的setXXX方法。获得ApplicationCotext和依赖注入的工作都在DelegatingRequestProcessor中完成。

    3,全权委托:Action 的创建和对象的依赖注入全部由IOC容器来完成。使用SpringDelegatingAcionProxy来帮助实现代理的工作 org.springframework.web.struts.DelegatingActiongProxy继承于org.apache.struts.action.Action .

    全权委托的配置方式同 方式 2 类似 (applcationContext.xml文件的配置和 Action类的实现方式相同)<struts-config><data-sources /><form-beans >    <form-bean name="loginForm" type="com.cao.struts.form.LoginForm" />

    </form-beans>

    <global-exceptions /><global-forwards /><action-mappings >    <!-- type指向的是spring 的代理类 -->    <action      attribute="loginForm"      input="login.jsp"      name="loginForm"      path="/login"      scope="request"

         type="org.springframework.web.struts.DelegatingActionProxy" >            <forward name="success" path="/ok.jsp" />      <forward name="error" path="/error.jsp" />    </action></action-mappings>  <message-resources parameter="com.cao.struts.ApplicationResources" />

    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">   <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/></plug-in>

    </struts-config>

    不同之处1 <action> type指向的是spring 的代理类

    2, 去掉struts-config.xml <controller >

     

    4.使用 org.springframework.web.struts.AutowiringRequestProcessor ,它是spring2.0 后加入的。类似DelegatingRequestProcessor,但它的自动完成能力更强。首先也要在struts-config.xml中用<controller>声明:........................<controller processorClass="org.springframework.web.struts.AutowiringRequestProcessor"/>其他的照常就得了,它比DelegatingRequestProcessor少了在spring配置中声明actionbean,它会根据action类名来自动完成注入操作。即StrutsAction中如有某业务类A对象的属性,又在spring配置文件中声明了业务类ABean,则AutowiringRequestProcessor就会自动将业务类ABean注入到Action中。看上去你好像啥都没做,而事实上,注入工作已经由AutowiringRequestProcessor自动完成。这种autowire的注入支持两种不同的方式,分别是byNamebyType,默认是byType。有时byType可能不起作用

    The default is "byType", matching service layer beans with the Action's bean property argument types. This behavior can be changed through specifying an "autowire" init-param for the Struts ActionServlet with the value "byName", which will match service layer bean names with the Action's bean property names.详情见spring2.5API中的这个类。

    三种整和方式中我们优先选用 全权委托的方式。理由:1,第一种使得过多的耦合了SpringAction .2,RequestProcessor类已经被代理 如果要再实现自己的实现方式(如:编码处理)怕有点麻烦。

    总结一下:整合工作中的步骤:1,修改struts-config.xml   2, 配置applicationContext.xml3, Action添加get/set方法 来获得依赖注入的功能

    2.hibernatespring的整合

    方法一:非托管。使用spring的配置文件引入hibernate的配置文件hibernate.cfg.xml

    applicationContext.xml中的写法:

        <bean id="sessionFactory"

           class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

           <property name="configLocation"

           value="classpath:hibernate.cfg.xml">

           </property>

           </bean>

    方法二:完全托管。抛弃hibernate的配置文件,完全由spring来管理。同时使用第三方的连接池。

    <bean id="dataSource"

           class="org.apache.commons.dbcp.BasicDataSource">

           <property name="driverClassName"

               value="com.microsoft.sqlserver.jdbc.SQLServerDriver">

           </property>

           <property name="url"

               value="jdbc:sqlserver://lab303/accp:1438;databaseName=zf">

           </property>

           <property name="username" value="sa"></property>

           <property name="password" value="sa"></property>

        </bean>

       

        <bean id="sessionFactory"

           class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

           <property name="dataSource" ref="dataSource" />

           <property name="mappingResources">

               <list>

                  <value>org/wang/ssh/entity/Tuser.hbm.xml</value>

               </list>

           </property>

           <property name="hibernateProperties">

               <props>

                  <prop key="hibernate.dialect">

                      org.hibernate.dialect.SQLServerDialect

                   </prop>

                  <prop key="hibernate.show_sql">true</prop>

               </props>

           </property>

     

        </bean>

    注意:这两种方法有一定的区别,第一种方式使用的hibernate自带的连接池,本身存在一些问题,官方建议使用第三方的连接池,如dbcp,c0p3等。这种方式默认单条增删改语句时,不会自动提交给数据库,必须显示的写出事务。或在hibernate.cfg.xml中配置自动提交事务。而方法二,单条增删改语句执行自动提交事务,可以不必显示声明事务。引起的原因是两者使用的数据源(连接池不同)。

     


    最新回复(0)