关于整合Struts2+Spring2+ Hibernate3

本文详细介绍了Spring2、Hibernate3及Struts2的整合过程,包括数据库连接池配置、事务管理、Spring声明式事务配置及Struts2与Spring的集成等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人接触SSH2项目时间短暂,所以在整合方面遇到了一些麻烦,下面将分享一下我的整合方案。
首先说一下Spring2 与 Hibernate3:
原理是省掉hibernate自带的sessionFactory 和dataSource 让spring来接管
因些步骤如下:

1.去掉hibernate的hibernate.hbm.xml文件
2.在spring的ApplicationContext.xml中加入如下代码,例如:

  1. <bean id="dataSource"
  2.         class="org.apache.commons.dbcp.BasicDataSource"
  3.         destroy-method="close">
  4.         <property name="driverClassName">
  5.             <value>com.mysql.jdbc.Driver</value>
  6.         </property>
  7.         <property name="url">
  8.             <value>jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8</value>
  9.         </property>
  10.         <property name="username">
  11.             <value>root</value>
  12.         </property>
  13.         <property name="password">
  14.             <value>demo</value>
  15.         </property>
  16.     </bean>
  17.     <bean id="sessionFactory"
  18.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  19.         <property name="dataSource">
  20.             <ref local="dataSource" />
  21.         </property>
  22.         <property name="mappingResources">
  23.             <list>              
  24.                 <value>com/demo/ssh2/hibernate/beans/Person.hbm.xml</value>
  25.             </list>
  26.         </property>
  27.         <property name="hibernateProperties">
  28.             <props>
  29.                 <prop key="hibernate.dialect">
  30.                     org.hibernate.dialect.MySQLDialect
  31.                 </prop>
  32.                 <prop key="hibernate.show_sql">true</prop>
  33.                 <prop key="hibernate.cache.use_query_cache">true</prop>
  34.                 <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
  35.             </props>
  36.         </property>
  37.     </bean>
  38.     <bean id="transactionManager"
  39.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  40.         <property name="sessionFactory">
  41.             <ref local="sessionFactory" />
  42.         </property>
  43.     </bean>

最后可以再加上spring的声明式事务

  1.     <bean id="PersonDAOProxy"
  2.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  3.         <property name="transactionManager">
  4.             <ref bean="transactionManager" />
  5.         </property>
  6.         <property name="target">
  7.             <ref local="personDAO" />
  8.         </property>
  9.         
  10.         <property name="transactionAttributes">
  11.             <props>
  12.                 <prop key="insert*">PROPAGATION_REQUIRED</prop>
  13.                 <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
  14.             </props>
  15.         </property>
  16.         
  17.     </bean>

还可以设置不同的TransactionInterceptor来得到更多的管理细节。如:

  1. <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">     
  2.    <property name="transactionManager" ref="transactionManager"></property>     
  3.    <property name="transactionAttributes">     
  4.     <props>     
  5.      <prop key="save*">PROPAGATION_REQUIRED</prop>     
  6.      <prop key="add*">PROPAGATION_REQUIRED</prop>     
  7.      <prop key="set*">PROPAGATION_REQUIRED</prop>     
  8.     <prop key="update*">PROPAGATION_REQUIRED</prop>     
  9.      <prop key="delete*">PROPAGATION_REQUIRED</prop>     
  10.     <prop key="remove">PROPAGATION_REQUIRED</prop>             
  11.      <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>     
  12.      <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>     
  13.     </props>     
  14.    </property>     
  15.  </bean> 

bean id="PersonDAOProxy" 中可以将:

<property name="transactionAttributes">换成

  1. <property name="interceptorNames">
  2.    <list>
  3.         <value>transactionInterceptor</value>
  4.    </list>
  5. </property>
还可以有更多的TransactionInterceptor

ok,spring2与hibernate3整合完成.

spring2整合struts2在书上见有多种方法,说一下我用过的两种方法
1.将struts2-spring-plugin-2.0.11.2.jar加入到lib目录下。
2.修改web.xml文件加入spring的监听器

  1.     <listener>
  2.         <listener-class>
  3.             org.springframework.web.context.ContextLoaderListener
  4.         </listener-class>
  5.     </listener>
配置文件位置:
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

再加上Stuts2的FilterDispatcher

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


3.修改struts.xml

  1.    <package name="newsViewDemo" extends="struts-default.xml" namespace="/view">
  2.         <action name="newsView!*" class="newsViewAction" method="{1}">
  3.             <result name="input">list.jsp</result>
  4.             <result name="detail">detail.jsp</result>
  5.             <result name="classnewslist">classnewslist.jsp</result>
  6.         </action>
  7.     </package>

同时在ApplicationContext.xml要有:

  1.     <bean id="newsViewAction" class="com.test.news.action.NewsViewAction"
  2.         abstract="false" lazy-init="default" autowire="default"
  3.         dependency-check="default">
  4.         <property name="newsListManager">
  5.             <ref bean="newsListManager" />
  6.         </property>
  7.     </bean>

这里要注意的是action的class属性值要与bean的id值相同
这样spring就可以管理action了,但是有没有发觉,把action同时在两个地方进行配置,如果以后action越来越多的话,会变的非常冗长,可不可以只写一边呢,答案是可以的方法如下:
现在呢,省去spring中action的配置利用spring的自动装配功能来实现
把struts.xml文件中action的class属值写成实际的class, 然后加上

  1. <constant name="struts.objectFactory.spring.autoWire" value="name"/>
至于action中的setXX方法和属性的声明我这里就不提了。

至此struts2与spring2也整合完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值