`
love19820823
  • 浏览: 936751 次
文章分类
社区版块
存档分类
最新评论

spring.net tx:advice 和 aop:config 配置事务 匹配名字的方法管理事务

 
阅读更多

在网上找到的都是java里的配置方式,后来认真读了下spring.net的帮助文档,解决了这个问题:现在把我的server层的配置文件copy出来:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:aop = "http://www.springframework.net/aop"
xmlns:tx="http://www.springframework.net/tx">

<!--fun list-->
<object id="userFunServer" type="com.elitel.im.bl.system.impl.UserFunImpl, com.elitel.im.bl">
<property name="UserDao" ref="userDao"/>
</object>

<!--自定义驱动方式(根据方法的名字判断事务的类型)-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- the transactional semantics...-->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="Get*" read-only="true"/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="Do*"/>
</tx:attributes>
</tx:advice>

<object id="serviceOperation" type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut, Spring.Aop">
<property name="pattern" value="com.elitel.im.bl.*"/>
</object>

<aop:config>
<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
</aop:config>

</objects>

主要是<property name="pattern" value="com.elitel.im.bl.*"/>节点的配置,表示匹配com.elitel.im.bl下所有的类及方法。

我把spring.net的帮助文档里相关的部分也copy出来:

You can also define the transactional semantics you want to apply through the use of a <tx:advice> definition. This lets you define the transaction metadata such as propagation and isolation level as well as the methods for which that metadata applies external to the code unlike the case of using the transaction attribute. The <tx:advice> definition creates an instance of a ITransactionAttributeSource during parsing time. Switching to use <tx:advice> instead of <tx:attribute-driven/> in the example would look like the following

<tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
    <tx:method name="Save*"/>   
    <tx:method name="Delete*"/>  
  </tx:attributes>
</tx:advice>

This says that all methods that start with Save and Delete would have associated with them the default settings of transaction metadata. These default values are listed below..

Here is an example using other elements of the <tx:method/> definition

  <!-- the transactional advice (i.e. what 'happens'; see the <aop:advisor/> object below) -->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!-- the transactional semantics... -->
    <tx:attributes>
      <!-- all methods starting with 'get' are read-only -->
      <tx:method name="Get*" read-only="true"/>
      <!-- other methods use the default transaction settings (see below) -->
      <tx:method name="*"/>
    </tx:attributes>
  </tx:advice>

The <tx:advice/> definition reads as “... all methods on starting with 'Get' are to execute in the context of a read-only transaction, and all other methods are to execute with the default transaction semantics”. The 'transaction-manager' attribute of the <tx:advice/> tag is set to the name of the PlatformTransactionManager object that is going to actually drive the transactions (in this case the 'transactionManager' object).

You can also use the AOP namespace <aop:advisor> element to tie together a pointcut and the above defined advice as shown below.

<object id="serviceOperation" type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut, Spring.Aop">
    <property name="pattern" value="Spring.TxQuickStart.Services.*"/>
</object>

<aop:config>
     
    <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>

</aop:config>

This is assuming that the service layer class, TestObjectManager, in the namespace Spring.TxQuickStart.Services. The <aop:config/> definition ensures that the transactional advice defined by the 'txAdvice' object actually executes at the appropriate points in the program. First we define a pointcut that matches any operation defined on classes in the Spring.TxQuickStart.Services (you can be more selective in your regular expression). Then we associate the pointcut with the 'txAdvice' using an advisor. In the example, the result indicates that at the execution of a 'SaveTwoTestObjects' and 'DeleteTwoTestObject', the advice defined by 'txAdvice' will be run.



分享到:
评论

相关推荐

    基于框架的Web开发-基于注解的事务配置.doc

    tx:advice和aop config的配置全部注释掉。 其余和切面、通知有关的配置都注释掉,或者删掉这个属性值引用前面定义的transactionManager bean添加保留 其余和切面、通知有关的配置都注释掉,或者删掉 这个属性值引用...

    JTA事务源码示例

    Spring+iBatis+JOTM实现JTA事务: 如何处理跨库事物:spring + jtom 的jta事务是个很好的选择. 这个源码示例非常不错,包括所有的源码和jar包,下载后eclipse 或 myeclipse 导入就能用。 里面有详细的说明和注释,...

    spring applicationContext 配置文件

    -- 配置事务管理器,這個事務性是爭對pdm數據庫的 --&gt; &lt;bean id="transactionManagerPdm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager " p:dataSource-ref="dataSourcePdm"/&gt; ...

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    application.xml配置 &lt;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=...

    Maven拆分代码.zip

    --配置事务管理器--&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; &lt;!--配置事务的通知--&gt; &lt;tx:advice id="advice"&gt; &lt;tx:attributes&gt; &lt;tx:...

    springmvc-ibatis

    -- 配置那个类那个方法用到事务处理 --&gt; &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt; &lt;tx:attributes&gt; &lt;tx:method name="get*" read-only="true"/&gt; &lt;tx:method name="add*" ...

    ssh框架在application.xml中配置数据源所需jar

    -- 配置事务管理器 --&gt; &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;/property&gt; &lt;!-- 配置事务的传播特性 --&gt; &lt;tx:advice id=...

    SSH第7章上机.zip ACCP8.0

    -- 创建事务管理器(spring针对hibernate实现的事务管理的切面类) --&gt; &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt; &lt;!-- 事务的通知类型 -...

    SpringMVC+Hibernate全注解整合

    -- 配置那个类那个方法用到事务处理 --&gt; &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt; &lt;tx:attributes&gt; &lt;tx:method name="get*" read-only="true" /&gt; &lt;tx:method name="add*" ...

    springmvcmybatis

    -- (事务管理)transaction manager, use JtaTransactionManager for global tx --&gt; class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; &lt;!-- 配置事务通知属性 --&gt; &lt;tx:...

    SpringMVC-SSH全注解

    -- 配置那个类那个方法用到事务处理 --&gt; &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt; &lt;tx:attributes&gt; &lt;tx:method name="get*" read-only="true" /&gt; &lt;tx:method name="add*" ...

    三大框架下分页源代码

    三大框架分页的实现: DAO里写法: //查出页面要显示的字段 -----分页代码 页面上的查询全部 ... &lt;property name="newsclassService"&gt; //这个名字要和action里定义的那个成员变量对应 &lt;/bean&gt;

    spring3.2+strut2+hibernate4

    -- JTA环境的事务配置 &lt;bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager or WebLogicJtaTransactionManager"/&gt; --&gt; &lt;!-- 使用annotation定义事务 --&gt; &lt;tx:...

Global site tag (gtag.js) - Google Analytics