spring 的3种事务配置方式

第一种TransactionProxyFactoryBean
缺点 配置文件多,对每个service都要配置
	<bean id="transactionBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
		<property name="transactionManager" ref="transactionManager"></property>
		<property name="transactionAttributes">
			<props>
				<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="get*">PROPAGATION_NEVER</prop>
			</props>
		</property>
	</bean>
	
	<bean id="userDao" class="com.tristan.web.controller.dao.UserDAO">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<bean id="userManagerBase" class="com.tristan.web.controller.service.UserManager">
		<property name="userDao" ref="userDao"></property>
	</bean>

	<bean id="userManager" parent="transactionBase">
		<property name="target" ref="userManagerBase"></property>
	</bean>


第二种 tx:advice + aop:config
极大的减少了配置文件
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="delete" propagation="REQUIRED" />
			<tx:method name="update" propagation="REQUIRED" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:pointcut expression="execution (* com.tristan.web.service.*.*(..))"
			id="services" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="services" />
	</aop:config>

	<context:component-scan base-package="com.tristan.web.service" />
	<context:component-scan base-package="com.tristan.web.dao" />


第三种 Annotation
和上面一样都是最好的选择
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
	<context:component-scan base-package="com.tristan.web.service" />
	<context:component-scan base-package="com.tristan.web.dao" />

@Transactional

你可能感兴趣的:(spring)