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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 结对编程1

    结对编程1

    2022年3月2日
    33
  • 鸿蒙OS架构及关键技术整理

    鸿蒙OS架构及关键技术整理鸿蒙OS架构及关键技术整理一. 鸿蒙OS整体介绍二. 子系统架构三. 关键技术1.分布式架构首次用于终端OS,实现跨终端无缝协同体验2.确定时延引擎和高性能IPC技术实现系统天生流畅3.基于微内核架构重塑终端设备可信安全4.通过统一IDE支撑一次开发,多端部署,实现跨终端生态共享四. 参考资料一. 鸿蒙OS整体介绍HarmonyOS简介原作者:xiangzhihong8前两天,华为发布了HarmonyOS2.0,俺也赶个时髦,给大家简单介绍下HarmonyOS。定义首先,我们来看一下官

    2022年7月12日
    9
  • IDEA 2021.7.21 激活码【中文破解版】

    (IDEA 2021.7.21 激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月22日
    49
  • webstorm怎么激活破解方法

    webstorm怎么激活破解方法,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月15日
    39
  • 完全二叉树图例_二叉树遍历图解

    完全二叉树图例_二叉树遍历图解画解二叉搜索树

    2022年10月21日
    0
  • FDD还是TDD?

    FDD还是TDD?达到更高频谱利用率、覆盖率,同时保证多媒体应用的QoS服务质量,已经成为第四代蜂窝4G网络的挑战和目标。在4G系统里,有许多关于物理层和多接入以提高频谱利用率方面的研究,以支持高达100Mbps甚至更高的数据传输速率。例如,正交频分多址OFDMA、MIMO天线,以及跨层资源优化,被认为是4G系统中的核心技术,并同时在频率选择的衰落信道中提供高可靠通信。另一方面,4G系统双工方式的选择,是FDD

    2022年5月8日
    45

发表回复

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

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