zoukankan      html  css  js  c++  java
  • Spring.NET的AOP怎么玩

    之前公司一直不让使用第三方组件,因此AOP方面的组建一直不能使用,很多面向切面的应用只能通过自己写一些GenericMethod的泛型方法来解决,有一些呆板。由于公司已经开始全面转Java,因此架构组放开了第三方组件的使用,这儿将对Spring.NET进行一个基础的学习。该项目虽然有1年都没有更新了(也反映了.NET品台热度的下降),但可以为未来使用JAVA最一定的铺垫,因此还是决定干了。

    Spring.NET文档及官方地址:http://www.springframework.net/documentation.html

    版本选择:1.3.2,创建日期为20110801.蛋蛋的忧伤。

    Spring AOP基本原理:使用代理模式实现

    这部分主要涉及两部分的内容,一种是通过代码添加Advices,一种是通过配置,推荐后者。

    • 应用建议(Applying advice):应用于类中所有方法,粒度太粗。
       1 public class ConsoleLoggingAroundAdvice : IMethodInterceptor
       2 {
       3 public object Invoke(IMethodInvocation invocation)
       4 {
       5 Console.Out.WriteLine("Advice executing; calling the advised method...");
       6 object returnValue = invocation.Proceed();
       7 Console.Out.WriteLine("Advice executed; advised method returned " + returnValue);
       8 return returnValue;
       9 }
      10 }
      11  
      12 public interface ICommand
      13 {
      14 object Execute(object context);
      15 }
      16 
      17 public class ServiceCommand : ICommand
      18 {
      19 public object Execute(object context)
      20 {
      21 Console.Out.WriteLine("Service implementation : [{0}]", context);
      22 return null;
      23 }
      24 }
      25 
      26 [TestMethod]
      27 public void TestMethod1()
      28 {
      29 ProxyFactory factory = new ProxyFactory(new ServiceCommand());
      30 factory.AddAdvice(new ConsoleLoggingAroundAdvice());
      31 ICommand command = (ICommand)factory.GetProxy();
      32 command.Execute("This is the argument");
      33 //ICommand command = (ICommand)ctx["myServiceObject"];
      34 //command.Execute("This is the argument");
      35 }
      View Code

    Using Pointcuts应用切入点:可以控制为方法级别的粒度,实际中最常用,这儿介绍配置的方式。

    <object id="consoleLoggingAroundAdvice"
    type="Spring.Aop.Support.RegularExpressionMethodPointcutAdvisor">
    <property name="pattern" value="Do"/>
    <property name="advice">
    <object type="Bjork.BaseService.BL.ConsoleLoggingAroundAdvice"/>
    </property>
    </object>
    <object id="myServiceObject"
    type="Spring.Aop.Framework.ProxyFactoryObject">
    <property name="target">
    <object id="myServiceObjectTarget"
    type="Bjork.BaseService.BL.ServiceCommand"/>
    </property>
    <property name="interceptorNames">
    <list>
    <value>consoleLoggingAroundAdvice</value>
    </list>
    </property>
    </object>
    • 接下来介绍其他的拦截器

     Before advice

    IMethodBeforeAdvice

    Before(MethodInfo method, object[] args, object target)

    After advice

    IAfterReturningAdvice

    AfterReturning(object returnValue, MethodInfo method, object[] args, object target)

    Throws advice

    IThrowsAdvice

    AfterThrowing(Exception ex)

    Around advice

    IMethodInterceptor

    Invoke(IMethodInvocation invocation)

    • Layering advice层次化建议(组合使用interceptor)
     1 //代码部分
     2 ProxyFactory factory = new ProxyFactory(new ServiceCommand());
     3 factory.AddAdvice(new ConsoleLoggingBeforeAdvice());
     4 factory.AddAdvice(new ConsoleLoggingAfterAdvice());
     5 factory.AddAdvice(new ConsoleLoggingThrowsAdvice());
     6 factory.AddAdvice(new ConsoleLoggingAroundAdvice());
     7 ICommand command = (ICommand) factory.GetProxy();
     8 command.Execute();
     9 
    10  //配置部分
    11 <object id="throwsAdvice" type="Spring.Examples.AopQuickStart.ConsoleLoggingThrowsAdvice"/>
    12 <object id="afterAdvice" type="Spring.Examples.AopQuickStart.ConsoleLoggingAfterAdvice"/>
    13 <object id="beforeAdvice" type="Spring.Examples.AopQuickStart.ConsoleLoggingBeforeAdvice"/>
    14 <object id="aroundAdvice" type="Spring.Examples.AopQuickStart.ConsoleLoggingAroundAdvice"/>
    15 <object id="myServiceObject"
    16 type="Spring.Aop.Framework.ProxyFactoryObject">
    17 <property name="target">
    18 <object id="myServiceObjectTarget"
    19 type="Spring.Examples.AopQuickStart.ServiceCommand"/>
    20 </property>
    21 <property name="interceptorNames">
    22 <list>
    23 <value>throwsAdvice</value>
    24 <value>afterAdvice</value>
    25 <value>beforeAdvice</value>
    26 <value>aroundAdvice</value>
    27 </list>
    28 </property>
    29 </object>
    View Code

     AOP的使用场景:缓存[Caching],性能监控,重试规则。

    这部分内容就到此为止,还有其他事宜,这个暂时就不使用了,确实存在适用性上的问题。不像JAVA Spring一样的整合使用,确实使用性下降很多,比如不是所有的场景都适合使用容器,且会增加系统的复杂程度。

  • 相关阅读:
    8.C++-类的关键字
    BFS-九宫格重排(详解)
    7.C++类与封装的概念
    4.数码相框-freetype多行显示,居中显示
    3.数码相框-通过freetype库实现矢量显示
    Linux-iconv命令、并批处理将所有GBK文件转换为UTF8(18)
    2.数码相框-编码(ASCII/GB2312/Unicode)介绍,并使LCD显示汉字字符(2)
    1.数码相框-相框框架分析(1)
    Kotlin 之操作符重载
    Andorid-解决View重复点击的思路
  • 原文地址:https://www.cnblogs.com/xiong2ge/p/springnet_aop.html
Copyright © 2011-2022 走看看