web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<description>struts/spring/hibernate</description>
<display-name>struts/spring/hibernate整合</display-name>
<context-param>
<description>配置多个spring配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/dao_service- bean.xml,/WEB-INF/action-form.xml</param-value>
</context-param>
<filter>
<description>使用spring里过滤器过器中文</description>
<filter-name>charset</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter>
<description>使用spring关闭hibernte里的session对象</description>
<filter-name>closeHibernateSession</filter-name>
<filterclass>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ValidatorLogin</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<!-- 过滤中文 -->
<filter-name>charset</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<!-- 关闭hiberntae里的Session对象 -->
<filter-name>closeHibernateSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<description>配置临听器加载spring容器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
struts-config.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- 替换控制器 -->
<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
<message-resources parameter="com.yohn.struts.ApplicationResources" />
<!-- 整合spring使用的插件
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in> -->
</struts-config>
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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- 配置hibernte数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="admin"></property>
<property name="initialSize" value="2"></property>
<property name="maxActive" value="10"></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--配置映身射文件-->
<property name="mappingResources">
<list>
<value>com/yohn/pojos/Deps.hbm.xml</value>
<value>com/yohn/pojos/Users.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<!-- 使用原有的hibenate配置文件
<property name="configLocation">
<value>classpath:/com/yohn/hibernate/hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
-->
</bean>
<!--通过spring窗口得一个HibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务管理器 apring2.0中间事务的配置和处理-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务属性:通知/处理/横切关注点
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" />
以add开头 事务传播属性(没有事务就创建) 事务隔离级别(数据库默认)
-->
<tx:advice id="tranAdivce">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="*" propagation="SUPPORTS" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- aop织入 -->
<aop:config>
<aop:advisor advice-ref="tranAdivce" pointcut="execution(* com.yohn.service.imp.*.*(..))"/>
</aop:config>
</beans>