Spirng AOP与事务配置记录

来源:互联网 发布:安心贷网络借贷平台 编辑:程序博客网 时间:2024/05/17 00:07

以下内容并不包括最新注解式的配置方法,为spring4.0,3.0,2.0适用的xml配置模式

Spring AOP与Transaction常见设置

<bean id="txDs" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    <property name="dataSource" ref="ds"></property>    </bean><tx:advice id="txAdvice" transaction-manager="txDs">    <tx:attributes>        <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" timeout="10000" read-only="false"/>    </tx:attributes></tx:advice><aop:config>    <aop:pointcut id="daopointcut" expression="execution(* app.aresxu.springweb.transaction.DaoSupportInterface.*(..))"/>    <aop:advisor advice-ref="txAdvice" pointcut-ref="daopointcut"/></aop:config>

顺序为 bean=》tx:advice=》aop, 
transaction基础类为 PlatformTransactionManager

也可以使用spring早期提供的TransationProxyFactoryBean实现事务切面

 <bean name="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">    <property name="transactionManager" ref="dataSourceTx"/>   <property name="transactionAttributes">     <props>       <prop key="insert*">PROPAGATION_REQUIRED</prop>       <prop key="update*">PROPAGATION_REQUIRED</prop>       <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>     </props>   </property> </bean>  <bean name="sqlmapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">    <property name="configLocation">            <value>classpath:ibatis/IbatisBean_config.xml</value>    </property>    <property name="dataSource">            <ref bean="dataSource"/>    </property>  </bean><bean name="internal_ibatisdao" class="com.web.dao.IbatisDao">    <property name="sqlmap" ref="sqlmapClient"></property></bean><bean name="ibatis_dao" parent="transactionProxy">    <property name="target" ref="internal_ibatisdao"/></bean>

也继承时可以修改方法事务等级:

<bean id="mySpecialService" parent="txProxyTemplate">    <property name="target">        <bean class="org.springframework.samples.MySpecialServiceImpl">        </bean>    </property>    <property name="transactionAttributes">        <props>            <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>            <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>            <prop key="store*">PROPAGATION_REQUIRED</prop>        </props>    </property></bean>


事务的PROPAGATION在org.springframework.transaction.TransactionDefinition 中定义 

有关参数方面 
aop:pointcut expression如下 

  • the execution of any public method:
execution(public * *(..))
  • the execution of any method with a name beginning with "set":
execution(* set*(..))
  • the execution of any method defined by the AccountService interface:
execution(* com.xyz.service.AccountService.*(..))
  • the execution of any method defined in the service package:
execution(* com.xyz.service..(..))
  • the execution of any method defined in the service package or a sub-package:
execution(* com.xyz.service...(..))

例如有: 
execution(* x.y.service.*Service.get(String,int)) and args(name, age) 等形式

tx:method参数配置

Table 11.1. <tx:method/> settings

AttributeRequired?DefaultDescription

name

Yes

 

Method name(s) with which the transaction attributes are to be associated. The wildcard (*) character can be used to associate the same transaction attribute settings with a number of methods; for example, get*handle*on*Event, and so forth.

propagation

No

REQUIRED

Transaction propagation behavior.

isolation

No

DEFAULT

Transaction isolation level.

timeout

No

-1

Transaction timeout value (in seconds).

read-only

No

false

Is this transaction read-only?

rollback-for

No

 

Exception(s) that trigger rollback; comma-delimited. For example, com.foo.MyBusinessException,ServletException.

no-rollback-for

No

 

Exception(s) that do not trigger rollback; comma-delimited. For example,com.foo.MyBusinessException,ServletException.



传统AOP配置有

<!-- this is the object that will be proxied by Spring's AOP infrastructure -->    <bean id="fooService" class="x.y.service.DefaultFooService"/>    <!-- this is the actual advice itself -->    <bean id="profiler" class="x.y.SimpleProfiler"/>    <aop:config>        <aop:aspect ref="profiler">            <aop:pointcut id="theExecutionOfSomeFooServiceMethod"                expression="execution(* x.y.service.FooService.getFoo(String,int))                and args(name, age)"/>            <aop:around pointcut-ref="theExecutionOfSomeFooServiceMethod"                method="profile"/>        </aop:aspect>    </aop:config>

package x.y;import org.aspectj.lang.ProceedingJoinPoint;import org.springframework.util.StopWatch;public class SimpleProfiler {    public Object profile(ProceedingJoinPoint call, String name, int age) throws Throwable {        StopWatch clock = new StopWatch("Profiling for " + name + " and " + age + "");        try {            clock.start(call.toShortString());            return call.proceed();        } finally {            clock.stop();            System.out.println(clock.prettyPrint());        }    }}

根据需要有before式,after式,around式及其他切入方法,这里是around

也可以使用早期spring提供的ProxyFactoryBean实现AOP

<bean id="personTarget" class="com.mycompany.PersonImpl">    <property name="name" value="Tony"/>    <property name="age" value="51"/></bean><bean id="myAdvisor" class="com.mycompany.MyAdvisor">    <property name="someProperty" value="Custom string property value"/></bean><bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor"></bean><bean id="person"    class="org.springframework.aop.framework.ProxyFactoryBean">    <property name="proxyInterfaces" value="com.mycompany.Person"/>    <property name="target" ref="personTarget"/>    <property name="interceptorNames">        <list>            <value>myAdvisor</value>            <value>debugInterceptor</value>        </list>    </property></bean>

使用这一类的ProxyAop 需要切面类继承一系列类(根据需要使用)

Advice是Advice型切面的超级接口,派生的子接口都有advice切面功能。

MethodInterceptor
BeforeAdvice 
ThrowsAdvice 
AfterReturningAdvice 
IntroductionInfo 
Advisor


0 0
原创粉丝点击