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


相关推荐

  • 基于IP地址划分VLAN

    基于IP地址划分VLAN实验环境:1、当检测IP在192.168.10.0./24时,PC接入交换机时,将其划分为VLAN10,且可以和VLAN10的服务器通信2、当检测IP在192.168.20.0/24时,PC接入交换机时,将其划分为VLAN20,且可以和VLAN20的服务器通信SW1<Huawei>system-view//进入全局配置模式[Huawei]undoinfo-centerenable//关闭信息告警提示[Huawei]sysnameSW1//

    2022年5月31日
    904
  • 程序员去外包公司有前途吗_程序员去外包是不是就废了

    程序员去外包公司有前途吗_程序员去外包是不是就废了虽然大部分人都抵制外包,但是很多人,尤其是萌新,并不清楚外包的主要缺点。我这里简单说一下。程序员去外包公司有前途吗?不能说去了外包公司就完全没有前途了,主要看个人能力,外包的工作内容,大多十分碎片化,甚至是机械化。因为如果这个工作内容真的很完整、成块儿,那正式工就做掉了。正式工做掉的理由有两个:完整工作内容有利于他,去构建业务认知。完整内容拆分出来外包,需要进行进行大量的沟通与团队协作,不利于整体效率。那么有没有办法避免碎片化呢?答案是有的。一方面可以表现出自身能力,获取正式团队

    2022年9月30日
    5
  • rubymine激活成功教程版_ruby gem

    rubymine激活成功教程版_ruby gem运行patcher.jar,找到rubymine安装目录下的lib目录的rubymine.jar打补丁

    2025年7月16日
    2
  • 配置Cisco MDS光纤存储交换机配置zone[通俗易懂]

    配置Cisco MDS光纤存储交换机配置zone[通俗易懂]配置CiscoMDS光纤存储交换机(MDSMultilayerDirectorSwitch多层光纤阵列交换机)大概流程:1:创建VSAN,将端口加入VSAN2:创建ZONE,将端口加入ZONE3:创建zoneSet,将ZONE加入set,并启用zoneSet4:启用所有端口使用命令行基本配置MDS-1(config)#sw…

    2025年6月16日
    3
  • 二维数组a[3][4]_树状数组和线段树的区别

    二维数组a[3][4]_树状数组和线段树的区别原题链接堆栈是一种经典的后进先出的线性结构,相关的操作主要有“入栈”(在堆栈顶插入一个元素)和“出栈”(将栈顶元素返回并从堆栈中删除)。本题要求你实现另一个附加的操作:“取中值”——即返回所有堆栈中元素键值的中值。给定 N 个元素,如果 N 是偶数,则中值定义为第 N/2 小元;若是奇数,则为第 (N+1)/2 小元。输入格式:输入的第一行是正整数 N(≤10​5​​ )。随后 N 行,每行给出一句指令,为以下 3 种之一:Push keyPopPeekMedian其中 key 是不超过

    2022年8月8日
    5
  • 免费申请国外免费域名超详细教程「建议收藏」

    免费申请国外免费域名超详细教程「建议收藏」1.首先申请免费域名网站:https://my.freenom.com/domains.php2.填入域名,这里我们以xcflag为列(尽量选择复杂一点的或者五个字母以上的域名,因为简单的有些域名是需要收费的),点击检查可用性。3.可以看到很多免费的域名(用的谷歌翻译插件,翻译有时候不是很准确,free翻译过来应该是免费而不是自由,之后会写一些关于谷歌插件的笔记,详细讲解)4.我们选择xcflag.tk点击立即获取,稍等一会点击购物车查看绿色按钮5.默认三个月试用,这里下拉框我们选择十二个月

    2022年6月30日
    130

发表回复

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

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