<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 自动扫描在某个包(包括子包)下面的所有类型,寻找包含特定注解的类,并将它注册到Spring中 -->
<context:component-scan base-package="cn.com.leadfar" />
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost/oa" />
<property name="username" value="root" />
<property name="password" value="sa" />
</bean>
<!-- 将SessionFactory交给Spring来管理 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>cn.com.leadfar.oa.model</value>
</list>
</property>
<property name="mappingResources">
<list>
<value>cn/com/leadfar/oa/model/Party.hbm.xml</value>
<value>cn/com/leadfar/oa/model/ACL.hbm.xml</value>
<value>cn/com/leadfar/oa/model/ActionResource.hbm.xml</value>
<value>cn/com/leadfar/oa/model/Menu.hbm.xml</value>
<value>cn/com/leadfar/oa/model/Role.hbm.xml</value>
<value>cn/com/leadfar/oa/model/User.hbm.xml</value>
<value>cn/com/leadfar/oa/model/UsersRoles.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
</value>
</property>
</bean>
<!-- Hibernate事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置哪些地方需要进行事务管理 -->
<aop:config>
<aop:pointcut id="allServiceMethods"
expression="execution(* cn.com.leadfar.oa.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="allServiceMethods" />
</aop:config>
<!-- 配置事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有save开头的方法,都使用事务 -->
<tx:method name="save*" />
<!-- 所有del开头的方法,都使用事务 -->
<tx:method name="del*" />
<tx:method name="add*" />
<tx:method name="update*" />
<tx:method name="rebuild*" />
<!--
所有其它方法,都使用事务,而且是只读的,即不允许在这些方法中做CUD操作!
FlushMode - AUTO/MANUAL
-->
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
</beans>
-============================================================================
<aop:config>
<!-- pointcut表示拦截什么,advice-ref表示拦截之后调用什么 -->
<aop:advisor advice-ref="TransactionManager" pointcut="execution(* cn.com.leadfar.spring.service.*.*(..))"/>
</aop:config>
package cn.com.leadfar.interceptors;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;
//@Component
public class TransactionManager implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
//之前干点什么
System.out.println("open session,begin transaction");
try {
//继续往下
Object returnValue = invocation.proceed();
System.out.println("commit transaction");
return returnValue;
} catch (RuntimeException e) {
System.out.println("rollback transaction!");
e.printStackTrace();
} finally{
System.out.println("close session");
}
return null;
}
}