Spring.NET学习笔记15——AOP的配置(基础篇) Level 200

  上篇我学习了Spring.NET的四种通知类型,AOP的实现方案比较复杂,是通过代码实现的。而Spring.NET框架给我们提供了配置的方式来实现AOP的功能。到目前为止,我们已经讨论过使用ProxyFactoryObject或其它类似的工厂对象显式创建AOP代理的方法。如果应用程序需要创建很多AOP代理,比如当需要代理某个服务层的所有对象时,这种方法就会使配置文件变的相当庞大。为简化配置过程,Spring.NET提供了“自动代理”的功能,可以根据条件自动创建代理对象,也就是说,可以将多个对象分组以作为要代理的候选对象。自动代理使用起来比较简单和方便。我仔细分析了一下,提供的几种配置差异主要在于切入点的方式不同。目前我实现了三种切入点的配置方式。

  首先我们先来看一下准备环境。  

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    public class AroundAdvice : IMethodInterceptor
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    
{
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
public object Invoke(IMethodInvocation invocation)
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
{
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            Console.WriteLine(
"开始:  " + invocation.TargetType.Name + "." + invocation.Method.Name);
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            
object result = invocation.Proceed();
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            Console.WriteLine(
"结束:  " + invocation.TargetType.Name + "." + invocation.Method.Name);
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            
return result;
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        }

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    }

  

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    public interface IService
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    
{
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        IList FindAll();
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
void Save(object entity);
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    }

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    
public class CategoryService : IService
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    
{
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
public IList FindAll()
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
{
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            
return new ArrayList();
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        }

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
public void Save(object entity)
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
{
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            Console.WriteLine(
"保存:" + entity);
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        }

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    }

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    
public class ProductService : IService
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    
{
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
public IList FindAll()
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
{
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            
return new ArrayList();
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        }

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
public void Save(object entity)
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
{
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            Console.WriteLine(
"保存:" + entity);
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        }

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    }

 

  一、对象名称切入点ObjectNameAutoProxyCreator  

  ObjectNameAutoProxyCreator用特定的文本值或通配符匹配目标对象的名称,并为满足条件的目标对象创建AOP代理。该类支持模式匹配字符串,如:"*name","name*",”*name*和精确文本如"name"我们可以通过下面这个简单的例子了解一下自动代理的功能。

 

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
      <object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop">
        
<property name="ObjectNames">
          
<list>
            
<value>*Service</value>
          
</list>
        
</property>
        
<property name="InterceptorNames">
          
<list>
            
<value>aroundAdvice</value>
          
</list>
        
</property>
      
</object>

      
<object id="aroundAdvice" type="Common.AroundAdvice, Common"/>
      
      
<object id="categoryService" type="Service.ProductService, Service"/>
      
<object id="productService" type="Service.ProductService, Service"/>

 

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200class Program
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    
{
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
static void Main(string[] args)
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        
{
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            IApplicationContext ctx 
= ContextRegistry.GetContext();
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            IDictionary speakerDictionary 
= ctx.GetObjectsOfType(typeof(IService));
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            
foreach (DictionaryEntry entry in speakerDictionary)
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            
{
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200                
string name = (string)entry.Key;
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200                IService service 
= (IService)entry.Value;
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200                Console.WriteLine(name 
+ " 拦截: ");
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200                service.FindAll();
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200                Console.WriteLine();
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200                service.Save(
"数据");
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200                Console.WriteLine();
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            }

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200            Console.ReadLine();
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200        }

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200    }

 

  输出效果:图1
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200图1

 

  使用ObjectNameAutoProxyCreator经常需要对要拦截的方法进行筛选,这时我用到Spring.Aop.Support.NameMatchMethodPointcutAdvisor,稍微修改一下配置:

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
      <object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop">
        
<property name="ObjectNames">
          
<list>
            
<value>*Service</value>
          
</list>
        
</property>
        
<property name="InterceptorNames">
          
<list>
            
<value>aroundAdvisor</value>
          
</list>
        
</property>
      
</object>

      
<object id="aroundAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop">
        
<property name="Advice" ref="aroundAdvice"/>
        
<property name="MappedNames">
          
<list>
            
<value>Find*</value>
          
</list>
        
</property>
      
</object>

      
<object id="aroundAdvice" type="Common.AroundAdvice, Common"/>


  输出效果:图2
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200图2

  MappedNames的配置为:Find*,因此能够拦截到FindAll方法。

  二、正则表达式切入点RegularExpressionMethodPointcutAdvisorSdkRegularExpressionMethodPointcut
  DefaultAdvisorAutoProxyCreator类会在当前容器中自动应用满足条件的Advisor,而不用在自动代理Advisor的对象定义中包含特定的对象名。它既可以保持配置文件的一致性,又可避免ObjectNameAutoProxyCreator引起的配置文件的臃肿。

  先来说RegularExpressionMethodPointcutAdvisor。

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
      <object id="aroundAdvisor" type="Spring.Aop.Support.RegularExpressionMethodPointcutAdvisor, Spring.Aop">
        
<property name="advice" ref="aroundAdvice"/>
        
<property name="patterns">
          
<list>
            
<value>.*Find*.*</value>
          
</list>
        
</property>
      
</object>

      
<!--必须让Spring.NET容器管理DefaultAdvisorAutoProxyCreator类-->
      
<object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator, Spring.Aop"/>

      
<object id="aroundAdvice" type="Common.AroundAdvice, Common"/>

      
      
<object id="categoryService" type="Service.ProductService, Service"/>
      
<object id="productService" type="Service.ProductService, Service"/>

 

输出效果:图3
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200图3

  以上配置相对复杂一点。使用SdkRegularExpressionMethodPointcut的配置就相对简单的多,而项目中SdkRegularExpressionMethodPointcut也经常用到
  SdkRegularExpressionMethodPointcut只需要简单的配置一下通知和切入点就完成了。

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
      <object id="advisor" type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut, Spring.Aop">
        
<property name="pattern" value="Service.*"/>
      
</object>

      
<aop:config>
        
<aop:advisor pointcut-ref="advisor" advice-ref="aroundAdvice"/>
      
</aop:config>

      
<object id="aroundAdvice" type="Common.AroundAdvice, Common"/>

      
<object id="categoryService" type="Service.ProductService, Service"/>
      
<object id="productService" type="Service.ProductService, Service"/>

 

输出效果:图4

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200图4

 

  pattern属性为拦截表达式。Service.*的意思是,拦截Service命名空间下(包括子空间)的所有类。如果改为Service.*.Find*",意思为拦截Service命名空间下(包括子空间)的所有类以Find开头的方法或Service命名空间下以Find开头的所有类
输出效果:图5

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200图5

 

  三、属性切入点AttributeMatchMethodPointcutAdvisor
Spring.NET框架运行开发人员自定义属性,拦截标注带有特定属性的类中的方法。

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
    public class ConsoleDebugAttribute : Attribute
    {

    }

    
public class AttributeService : IService
    {
        [ConsoleDebug]
        
public IList FindAll()
        {
            
return new ArrayList();
        }

        
public void Save(object entity)
        {
            Console.WriteLine(
"保存:" + entity);
        }
    }

 

Spring.NET学习笔记15——AOP的配置(基础篇) Level 200
      <object id="aroundAdvisor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
        
<property name="Advice" ref="aroundAdvice"/>
        
<property name="Attribute"
                  value
="ConfigAttribute.Attributes.ConsoleDebugAttribute, ConfigAttribute" />
      
</object>
      
      
<object id="proxyFactoryObject" type="Spring.Aop.Framework.ProxyFactoryObject">
        
<property name="Target">
          
<object type="ConfigAttribute.Service.AttributeService, ConfigAttribute" />
        
</property>
        
<property name="InterceptorNames">
          
<list>
            
<value>aroundAdvisor</value>
          
</list>
        
</property>
      
</object>

      
<object id="aroundAdvice" type="Common.AroundAdvice, Common"/>

 

  输出效果:图6
Spring.NET学习笔记15——AOP的配置(基础篇) Level 200图6

 

  代码下载

 

 

  返回目录

 

作者:刘冬.NET 博客地址:http://www.cnblogs.com/GoodHelper/ 欢迎转载,但须保留版权