Spring.NET学习笔记(6)-基础AOP

Spring.NET学习笔记(6)-基础AOP

   

1.在我们的系统中,常常要对操作进行记录,比如说某某人新增了一笔数据,然后在数据库中增加一笔操作记录

2.前端开发人员往往会在ajax调用后端的时候,调用之前先做一些数据检验的工作,调用之后对于返回的数据ui做出一些反应

3.后端开发人员有时候会做一个数据的ing和ed事件操作,比如插入数据,InsertIng和Inserted事件.

以上的种种反应在进行一个方法的操作,往往还有着其他的关联,这被我们称之为耦合,系统越大,关联越多。再比如添加日志这个功能,某天就不要了,但添加日志的代码与新增数据的代码是写在一起的,这时候就必须做出修改。

对于这些问题,我们就可以用AOP来解决。关于AOP有很多概念,我们直接看代码,不多概念。

1.通知

(1)前后通知和抛错通知,以接口来实现

public class ConsoleLoggingBeforeAdvice : IMethodBeforeAdvice
{
    public void Before(MethodInfo method, object[] args, object target)
    {
        Console.Out.WriteLine("Intercepted call to this method : " + method.Name);
        Console.Out.WriteLine("    The target is       : " + target);
        Console.Out.WriteLine("    The arguments are   : ");
        if(args != null)
        {
            foreach (object arg in args)
            {
                Console.Out.WriteLine("\t: " + arg);
            }
        }
    }
}


public class ConsoleLoggingAfterAdvice : IAfterReturningAdvice
{
    public void AfterReturning(
        object returnValue, MethodInfo method, object[] args, object target)
    {
        Console.Out.WriteLine("This method call returned successfully : " + method.Name);
        Console.Out.WriteLine("    The target was      : " + target);
        Console.Out.WriteLine("    The arguments were  : ");
        if (args != null)
        {
            foreach (object arg in args)
            {
                Console.Out.WriteLine("\t: " + arg);
            }
        }
        Console.Out.WriteLine("    The return value is : " + returnValue);
    }
}


public class ConsoleLoggingThrowsAdvice : IThrowsAdvice
{
    public void AfterThrowing(Exception ex)
    {
        Console.Error.WriteLine(
            String.Format("Advised method threw this exception : {0}", ex.Message));
    }
}

执行每个方法时都会触发通知,注意当设置属性的时候也会触发(因为属性是伪方法)

// Create AOP proxy programmatically.
ProxyFactory factory = new ProxyFactory(new ServiceCommand());
factory.AddAdvice(new ConsoleLoggingBeforeAdvice());
factory.AddAdvice(new ConsoleLoggingAfterAdvice());
factory.AddAdvice(new ConsoleLoggingThrowsAdvice());
ICommand command = (ICommand)factory.GetProxy();

command.Execute();

(2)环绕通知

即以上三个通知的集合,还可以充当拦截器的作用,阻止方法触发

public class ConsoleLoggingAroundAdvice : IMethodInterceptor
{
    public object Invoke(IMethodInvocation invocation)
    {
        Console.Out.WriteLine(String.Format(
            "Intercepted call : about to invoke method '{0}'", invocation.Method.Name));

        object returnValue = invocation.Proceed();

        Console.Out.WriteLine(String.Format(
            "Intercepted call : returned '{0}'", returnValue));

        return returnValue;
    }
}

 

ProxyFactory factory = new ProxyFactory(new ServiceCommand());
              
factory.AddAdvice(new ConsoleLoggingAroundAdvice());

ICommand command = (ICommand)factory.GetProxy();

command.Execute();
if (command.IsUndoCapable)
{
    command.UnExecute();
}

 

2.以配置文件方式配置

<object id="aroundAdvice" 
        type="Spring.AopQuickStart.Aspects.ConsoleLoggingAroundAdvice, Spring.AopQuickStart.Common" />
<object id="throwsAdvice" 
        type="Spring.AopQuickStart.Aspects.ConsoleLoggingThrowsAdvice, Spring.AopQuickStart.Common" />

<object id="myServiceCommand" type="Spring.Aop.Framework.ProxyFactoryObject">
  <property name="Target">
    <object type="Spring.AopQuickStart.Commands.ServiceCommand, Spring.AopQuickStart.Common" />
  </property>
  <property name="InterceptorNames">
    <list>
      <value>aroundAdvice</value>
      <value>throwsAdvice</value>
    </list>
  </property>
</object>

3.切入点

即通知在何时(指在某个方法)执行,以上通知是不管调用什么属性和方法都通知,显然我们就需要某几个方法同志就好了,所以需要一个方法匹配的过滤,如下代码NameMatchMethodPointcutAdvisor可进行方法名字过滤,

<object id="aroundAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop">
        <property name="Advice">
          <object type="Spring.AopQuickStart.Aspects.ConsoleLoggingAroundAdvice, Spring.AopQuickStart.Common" /> </property> <property name="MappedNames"> <list> <value>*Execute</value> </list> </property> </object>

      <object id="throwsAdvice" 
              type="Spring.AopQuickStart.Aspects.ConsoleLoggingThrowsAdvice, Spring.AopQuickStart.Common" />

      <object id="myServiceCommand" type="Spring.Aop.Framework.ProxyFactoryObject">
        <property name="Target">
          <object type="Spring.AopQuickStart.Commands.ServiceCommand, Spring.AopQuickStart.Common" />
        </property>
        <property name="InterceptorNames">
          <list>
            <value>aroundAdvisor</value>
            <value>throwsAdvice</value>
          </list>
        </property>
      </object>


除了此过滤器,只要实现IPointcutAdvisor接口的都可以,spring还提供了其他的过滤类。

1.RegularExpressionMethodPointcutAdvisor  &&  SdkRegularExpressionMethodPointcut

正则表达式匹配

<object id="settersAndAbsquatulatePointcut"
    type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut, Spring.Aop">
  <property name="patterns">
    <list>
      <value>.*set.*</value>
      <value>.*absquatulate</value>
    </list>
  </property>
</object>


<object id="settersAndAbsquatulateAdvisor"
    type="Spring.Aop.Support.RegularExpressionMethodPointcutAdvisor, Spring.Aop">
  <property name="advice">
    <ref local="objectNameOfAopAllianceInterceptor"/>
  </property>
  <property name="patterns">
    <list>
      <value>.*set.*</value>
      <value>.*absquatulate</value>
    </list>
  </property>
</object>


2.AttributeMatchMethodPointcutAdvisor

在方法上挂标签匹配

<object id="aroundAdvisor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
  <property name="Advice">
    <object type="Spring.AopQuickStart.Aspects.ConsoleLoggingAdvice, Spring.AopQuickStart.Step4" />
  </property>
  <property name="Attribute" 
            value="Spring.AopQuickStart.Attributes.ConsoleLoggingAttribute, Spring.AopQuickStart.Step4" />
</object>


3.DynamicMethodMatcherPointcutAdvisor动态切入点,需要自定义

先到此为止

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110870.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 带通滤波器的主要功能和作用_带通滤波器的作用是什么

    带通滤波器的主要功能和作用_带通滤波器的作用是什么带通滤波器的工作原理与应用带通滤波器可以理解成为一个电子接口单元,这个单元可以将特定频率范围内的信号传输过去,而阻断这个频率范围以外的信号,达到选择性传输的目的。与此对应,滤波器可以分为低通滤波器,即某频率以下的信号可以传输过去。高通滤波器和带阻滤波器。这些功能都是通过特定电子原件按照不同的布置实现的。比如电容串联可以阻止低频率信号,导通高频率信号。而并联一个电容就可以实现将高频信号短路的功能。又…

    2022年9月5日
  • webpack打包优化面试_什么是webpack

    webpack打包优化面试_什么是webpackwebpack打包优化(polyfill/HappyPack/dllPlugin)

    2022年10月20日
  • 你有网站吗_有些网站进不去怎么办

    你有网站吗_有些网站进不去怎么办1.最强大的“免费电子书”下载区——新浪电子书共享http://ishare.iask.sina.com.cn/(数十万册哦,全是免费滴!)2.性能很好的免费网络硬盘——云盘网(10G免费超大空间哦,支持共享,可加密文件)http://www.diskes.com/3.爱酷美剧(一边看美剧,一边学英语,不耽误啊!)http://www.icoolen.com/film4.传说中“天涯第一镇山神贴”!!!(先说明白了。机子配置低的不要点开。会死机的。镇山贴可不是开玩笑的,配置低的,直接卡死在外面,配置

    2022年10月2日
  • AD域filter

    AD域filterAD域BuildingLDAPFiltershttp://flylib.com/books/en/1.434.1.37/1/ldapextensiblefilterhttp://ldapwiki.willeke.com/wiki/ExtensibleMatchhttp://ldapwiki.willeke.com/wiki/Active%20Direct

    2022年5月13日
  • SD卡与MMC卡的区别

    SD卡与MMC卡的区别本文译至:http://home.impress.co.jp/magazine/dosvpr/q-a/0108/qa0108_2.htm

    2022年6月12日
  • 在线名片设计_diy名片代码在线生成

    在线名片设计_diy名片代码在线生成小小名片,看似简单,它要经过以下八道工序才能到你手中。其间你还得参与名片制作的前期工作,你要对名片印刷方式、印刷难易、印刷用纸选择;你还得提供名片的具体内容,设计大致思路;大多数时间你还得要校稿,特别是要求较高的复杂的名片,商家都有如此请求。名片后期加工也较复杂,需要专业设备和熟练的操作人员。以往,我们得亲自前往名片印刷店印刷,一盒名片也许要去好多趟,好在有了互联网,现在简单了。你大可一边喝着咖啡…

    2022年10月25日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号