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一样的整合使用,确实使用性下降很多,比如不是所有的场景都适合使用容器,且会增加系统的复杂程度。

  • 相关阅读:
    关于WPF中Popup控件的小记
    javascript调用外部wpf的方法
    html+ashx 缓存问题
    『Linux学习笔记』8. 权限
    LeetCode 2.两数相加
    C# 标签打印示例 1
    检索COM 类工厂中CLSID 为 {0002450000000000C000000000000046}的组件时失败
    C# 文件操作(一)
    Nginx 事件基本处理流程分析
    Spring学习笔记1:概论
  • 原文地址:https://www.cnblogs.com/xiong2ge/p/springnet_aop.html
Copyright © 2011-2022 走看看