考虑一下这样的情景:你有许多服务层对象,你想对它们中的每一个应用完全不同的事务性配置。你可以通过定义不同的 <aop: advisor/> 元素来实现,这些元素具有不同的 pointcut 和 advice-ref 属性值。
作为比较,首先假设你所有的服务层类都定义在一个根 x.y.service包中。为了使所有作为定义在该包(或子包)中的类的实例,并且名字以 Service 结尾的 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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttps://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 对 service 包以及子包做切入点和顾问配置 --><aop:config><aop:pointcut id="serviceOperation"expression="execution(* x.y.service..*Service.*(..))"/><aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/></aop:config><!-- 这两个 bean 将是事务性的... --><bean id="fooService" class="x.y.service.DefaultFooService"/><bean id="barService" class="x.y.service.extras.SimpleBarService"/><!-- 下面两个 bean 不是事务性的--><bean id="anotherService" class="org.xyz.SomeService"/> <!-- (不在正确的包装内) --><bean id="barManager" class="x.y.service.SimpleBarManager"/> <!-- (不是以'Service' 结尾) --><!-- 配置 advice:定义 get 开头的方法是只读事务,其他的为默认事务 --><tx:advice id="txAdvice"><tx:attributes><tx:method name="get*" read-only="true"/><tx:method name="*"/></tx:attributes></tx:advice><!-- other transaction infrastructure beans such as a TransactionManager omitted... --></beans>
下面的例子显示了如何配置两个具有完全不同的事务性设置的不同 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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttps://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><aop:config><!-- 切入点:service 下所有以 Service 结尾的类 --><aop:pointcut id="defaultServiceOperation"expression="execution(* x.y.service.*Service.*(..))"/><!-- --><aop:pointcut id="noTxServiceOperation"expression="execution(* x.y.service.ddl.DefaultDdlManager.*(..))"/><aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="defaultTxAdvice"/><aop:advisor pointcut-ref="noTxServiceOperation" advice-ref="noTxAdvice"/></aop:config><!-- 这个Bean将是事务性的(见'defaultServiceOperation' 切入点) --><bean id="fooService" class="x.y.service.DefaultFooService"/><!-- 这个 bean 也将是事务性的,但有完全不同的事务性设置--><bean id="anotherFooService" class="x.y.service.ddl.DefaultDdlManager"/><tx:advice id="defaultTxAdvice"><tx:attributes><tx:method name="get*" read-only="true"/><tx:method name="*"/></tx:attributes></tx:advice><!-- 定义了事务传播为 NEVER --><tx:advice id="noTxAdvice"><tx:attributes><tx:method name="*" propagation="NEVER"/></tx:attributes></tx:advice><!-- other transaction infrastructure beans such as a TransactionManager omitted... --></beans>
