spring基于hibernate的事务管理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: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/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
	
	<aop:aspectj-autoproxy />
	 <!-- 开启注解扫描 -->
	 <context:component-scan base-package="com.aop">
	 </context:component-scan>
	 <!--基于hibernate的局部事务配置  -->
	 <bean  id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	 	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
   		<property name="url" value="jdbc:mysql://localhost:3306/banxian" />
   		<property name="username" value="root"></property>
   		<property name="password" value="545117953"></property>
   		<lookup-method />
	 </bean>
	 <bean  id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
	 	<property name="dataSource" ref="dataSource"></property>
	 	<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	 </bean>
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!--基于hibernate的JTA全局事务管理配置  -->
	<bean id="transActionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
	</bean>
	<!--配置事务管理器  -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 配置要使用事务的方法 -->
			<tx:method name="get*" read-only="true" timeout="-1" propagation="REQUIRED"/>
			<tx:method name="add*" read-only="false" rollback-for="java.lang.Exception"/>
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<!--配置一个切入点  -->
		<aop:pointcut expression="execution(* com.sevice.*Impl.*.*(..))" id="myPointcut"/>
		<!--指定myPointcut切入点应用 txAdvice事务增强处理 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
	</aop:config>
	<!-- 根据注解来生成事务代理 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

猜你喜欢

转载自blog.csdn.net/qq_37150258/article/details/80792048