spring的事务配置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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/p 
  http://www.springframework.org/schema/p/spring-p-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/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<!-- 配置hibernate.cfg.xml的外部引用 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml">
		</property>
	</bean>

	<!-- 配置事物管理 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>

	<!-- 事务拦截 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="mod*" propagation="REQUIRED" />
			<tx:method name="*" read-only="false" />
		</tx:attributes>
	</tx:advice>
	<!-- 配置AOP(Aspect Oriented Programming(面向切面编程)) -->
	<aop:config proxy-target-class="true">
		<!-- aop:pointcut切入点,advice通知,切入代码advisor -->
		<aop:pointcut id="point" expression="execution(* com.szdp.action.*.*(..))" />
		<aop:pointcut id="point2" expression="execution(* com.szdp.imp.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="point" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="point2" />
	</aop:config>

	<!-- 配置(IOC(Inversion of Control(控制反转,依赖注入))) -->
	<bean id="dbImp" class="com.szdp.imp.DBImp">
		<!-- property(属性注入方式) -->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="test" class="com.szdp.action.Test">
		<property name="dbImp" ref="dbImp"></property>
	</bean>
</beans>

相关推荐