通知事务操作

假设您想要同时运行事务操作和一些基本的性能分析建议。在<tx:annotation-driven/>的上下文中,您如何实现这一点?

当您调用updateFoo(Foo)方法时,您希望看到以下操作:

  • 配置的性能分析方面开始。

  • 事务建议运行。

  • 建议对象上的方法运行。

  • 事务提交。

  • 性能分析方面报告整个事务方法调用的确切持续时间。

本章不涉及详细解释AOP(除了它适用于事务)。有关AOP配置和AOP的详细内容,请参阅AOP

以下代码显示了之前讨论的简单性能分析方面:

  • Java

  • Kotlin

package x.y;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;
import org.springframework.core.Ordered;

public class SimpleProfiler implements Ordered {

	private int order;

	// 允许我们控制建议的顺序
	public int getOrder() {
		return this.order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	// 这个方法是环绕建议
	public Object profile(ProceedingJoinPoint call) throws Throwable {
		Object returnValue;
		StopWatch clock = new StopWatch(getClass().getName());
		try {
			clock.start(call.toShortString());
			returnValue = call.proceed();
		} finally {
			clock.stop();
			System.out.println(clock.prettyPrint());
		}
		return returnValue;
	}
}
package x.y

import org.aspectj.lang.ProceedingJoinPoint
import org.springframework.util.StopWatch
import org.springframework.core.Ordered

class SimpleProfiler : Ordered {

	private var order: Int = 0

	// 允许我们控制建议的顺序
	override fun getOrder(): Int {
		return this.order
	}

	fun setOrder(order: Int) {
		this.order = order
	}

	// 这个方法是环绕建议
	fun profile(call: ProceedingJoinPoint): Any {
		var returnValue: Any
		val clock = StopWatch(javaClass.name)
		try {
			clock.start(call.toShortString())
			returnValue = call.proceed()
		} finally {
			clock.stop()
			println(clock.prettyPrint())
		}
		return returnValue
	}
}

通过Ordered接口控制建议的顺序。有关建议顺序的详细信息,请参阅建议顺序

以下配置创建了一个fooService bean,对其应用了所需顺序的性能分析和事务方面:

<?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"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx
		https://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">

	<bean id="fooService" class="x.y.service.DefaultFooService"/>

	<!-- 这是方面 -->
	<bean id="profiler" class="x.y.SimpleProfiler">
		<!-- 在事务建议之前运行(因此顺序号较低) -->
		<property name="order" value="1"/>
	</bean>

	<tx:annotation-driven transaction-manager="txManager" order="200"/>

	<aop:config>
			<!-- 这个建议围绕事务建议运行 -->
			<aop:aspect id="profilingAspect" ref="profiler">
				<aop:pointcut id="serviceMethodWithReturnValue"
						expression="execution(!void x.y..*Service.*(..))"/>
				<aop:around method="profile" pointcut-ref="serviceMethodWithReturnValue"/>
			</aop:aspect>
	</aop:config>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
		<property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/>
		<property name="username" value="scott"/>
		<property name="password" value="tiger"/>
	</bean>

	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>

</beans>

您可以以类似的方式配置任意数量的其他方面。

以下示例创建了与前两个示例相同的设置,但使用了纯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"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx
		https://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd">

	<bean id="fooService" class="x.y.service.DefaultFooService"/>

	<!-- 性能分析建议 -->
	<bean id="profiler" class="x.y.SimpleProfiler">
		<!-- 在事务建议之前运行(因此顺序号较低) -->
		<property name="order" value="1"/>
	</bean>

	<aop:config>
		<aop:pointcut id="entryPointMethod" expression="execution(* x.y..*Service.*(..))"/>
		<!-- 在性能分析建议之后运行(参见order属性) -->

		<aop:advisor advice-ref="txAdvice" pointcut-ref="entryPointMethod" order="2"/>
		<!-- order值高于性能分析方面 -->

		<aop:aspect id="profilingAspect" ref="profiler">
			<aop:pointcut id="serviceMethodWithReturnValue"
					expression="execution(!void x.y..*Service.*(..))"/>
			<aop:around method="profile" pointcut-ref="serviceMethodWithReturnValue"/>
		</aop:aspect>

	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="*"/>
		</tx:attributes>
	</tx:advice>

	<!-- 其他<bean/>定义,如DataSource和TransactionManager -->

</beans>

上述配置的结果是一个fooService bean,对其应用了性能分析和事务方面,顺序如上。如果您希望性能分析建议在进入时在事务建议之后运行,在离开时在事务建议之前运行,您可以交换性能分析方面bean的order属性的值,使其高于事务建议的顺序值。

您可以以类似的方式配置其他方面。