Spring.net 间接调用被AOP拦截的方法失效(无法进入aop的拦截方法)

1.下面的tx要定义
<objects xmlns="http://www.springframework.net"
         xmlns:db="http://www.springframework.net/database"
          xmlns:tx="http://www.springframework.net/tx">
2.启用事务切面和类型代理
  <tx:attribute-driven transaction-manager="TxManager"  proxy-target-type="true"  />
3.使用代理工厂或者自动代理时要有这个属性,我注释起来给大家看下
<!--   <property name="ExposeProxy" value="true"></property>-->,还有这个xml也相当于注册advisor
<?xml version="1.0" encoding="utf-8" ?>
<objects  xmlns="http://www.springframework.net"  
          xmlns:db="http://www.springframework.net/database"
          xmlns:tx="http://www.springframework.net/tx">
  <object name="autoProxyCreator1" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop">
    <property name="InterceptorNames" value="transactionInterceptor"/>
    <property name="ExposeProxy" value="true"></property>
    <property name="ObjectNames">
      <list>
        <value>*Service</value>
        <value>*BLL</value>
        <!--拦截目标对象-->
      </list>
    </property>
    <property name="InterceptorNames">
      <list>
        <value>aroundAdvisor</value>
        <value>throwAdvisor</value>
        <value>prductGroupAdvisor</value>
        <value>beforeAdvisor</value>
      </list>
    </property>
  </object>
  <!--<object id="aroundAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop">
    <property name="Advice" ref="AroundAdvise"/>
    <property name="MappedNames">
      <list>
        <value>FindAll</value>
      </list>
    </property>
  </object>-->
</objects>
4.在间接调用有aop代理的方法的时候,这样写,虽然麻烦点但是有用,下面的saveorupdate方法是有用aop的
  if (AopContext.CurrentProxy != null)
            {
                     ((dynamic)AopContext.CurrentProxy).SaveOrUpdate(obj);
            }
            else
            {
                SaveOrUpdate(obj);
            }
5.顺便给出aop方法特性的定义以及saveorupdate方法的定义
    /// <summary>
    /// 前置通知
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, Inherited = true)]
    public class BeforeInterceptorAttribute : Attribute
    {
    }
 [BeforeInterceptor]
        public override BasAssetClass SaveOrUpdate(BasAssetClass obj)
        {
            //这个名称已经被人用了,重复新增的时候
            bool exits = GetTotalCount(x => x.AssetClassName == obj.AssetClassName && x.Id != obj.Id) > 0;
            if (exits)
            {
                throw new Exception("名称不能重复!!");
            }
            return base.SaveOrUpdate(obj); 
        }
6.advisor具体定义的xml
<?xml version="1.0" encoding="utf-8" ?>
<objects  xmlns="http://www.springframework.net"
          xmlns:db="http://www.springframework.net/database"
          xmlns:tx="http://www.springframework.net/tx">
  <!--注意aroundAdvisor,aroundAdvisor1必须先在AopConfigInterceptor.xml注册-->
  <object id="aroundAdvisor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
    <property name="Advice" ref="AroundAdvise"/>
    <property name="Attribute"
              value="AopBehavior.MethodInterceptorAttribute, AopBehavior" />
  </object>
   <object id="prductGroupAdvisor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
    <property name="Advice" ref="ProductGroupAroundAdvise"/>
    <property name="Attribute"
              value="AopBehavior.ProductGroupAttribute, AopBehavior" />
  </object>
  <object id="throwAdvisor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
    <property name="Advice" ref="ThrowsAdvise"/>
    <property name="Attribute"
              value="AopBehavior.ThrowExceptionAttribute, AopBehavior" />
  </object>
  <object id="beforeAdvisor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
    <property name="Advice" ref="BeforeAdvise"/>
    <property name="Attribute"
              value="AopBehavior.BeforeInterceptorAttribute, AopBehavior" />
  </object>
  <!--<object id="proxyFactoryObject" type="Spring.Aop.Framework.ProxyFactoryObject">
    <property name="Target">
      <object type="Bll.SysMenuOperationService, HRABLL" />
    </property>
    <property name="InterceptorNames">
      <list>
        <value>aroundAdvisor</value>
      </list>
    </property>
  </object>-->

</objects>

 

转载于:https://www.cnblogs.com/kexb/p/6244243.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP 失效的原因可能有很多,以下是一些可能的原因: 1. 未配置正确的切面表达式:切面表达式定义了应该在哪些方法上应用切面。如果切面表达式不正确,就无法正确地匹配到目标方法,并且切面将无法生效。 2. 目标方法没有被代理:Spring AOP 使用动态代理来实现切面,在运行时生成代理对象,并将切面应用于代理对象上的目标方法。如果目标方法没有被代理,切面将无法生效。 3. 目标方法被直接调用而不是通过代理调用:如果目标对象的方法被其他部分直接调用,而不是通过生成的代理对象调用,那么切面将无法生效。 4. 切面的顺序问题:在一个应用中可能存在多个切面,每个切面都可以定义相同或不同的切面表达式。如果切面的顺序不正确,可能会导致切面失效,例如一个切面将目标方法拦截之后,另一个切面再次拦截方法。 5. 使用错误的切面类型:Spring AOP 支持多种类型的切面,如前置通知、后置通知、异常通知等。如果使用了错误的切面类型,或者切面类型不匹配目标方法,切面将无法生效。 6. 目标方法没有被正确地扫描到:Spring AOP 使用自动扫描来发现目标方法,并将切面应用于这些方法上。如果目标方法没有被正确地扫描到,切面将无法生效。 总结来说,Spring AOP 失效的原因可能是由于切面表达式的问题、代理问题、调用方式问题、切面顺序问题、切面类型问题、以及扫描问题。为了确保切面生效,我们需要仔细检查配置,并理解Spring AOP的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值