Spring.NET教程(十三)——AOP的配置(基础篇)
首先我们先来看一下准备环境。
通知
public class AroundAdvice : IMethodInterceptor
{
public object Invoke(IMethodInvocation invocation)
{
Console.WriteLine("开始: " + invocation.TargetType.Name + "." + invocation.Method.Name);
object result = invocation.Proceed();
Console.WriteLine("结束: " + invocation.TargetType.Name + "." + invocation.Method.Name);
return result;
}
}
目标对象
public interface IService
{
IList FindAll();
void Save(object entity);
}
public class CategoryService : IService
{
public IList FindAll()
{
return new ArrayList();
}
public void Save(object entity)
{
Console.WriteLine("保存:" + entity);
}
}
public class ProductService : IService
{
public IList FindAll()
{
return new ArrayList();
}
public void Save(object entity)
{
Console.WriteLine("保存:" + entity);
}
}
一、对象名称切入点:ObjectNameAutoProxyCreator
ObjectNameAutoProxyCreator可以用特定的文本值或通配符匹配目标对象的名称,并为满足条件的目标对象创建AOP代理。该类支持模式匹配字符串,如:"*name","name*",”*name*“和精确文本如"name"。我们可以通过下面这个简单的例子了解一下自动代理的功能。
App.config
<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"/>
Program
class Program
{
static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
IDictionary speakerDictionary = ctx.GetObjectsOfType(typeof(IService));
foreach (DictionaryEntry entry in speakerDictionary)
{
string name = (string)entry.Key;
IService service = (IService)entry.Value;
Console.WriteLine(name + " 拦截: ");
service.FindAll();
Console.WriteLine();
service.Save("数据");
Console.WriteLine();
}
Console.ReadLine();
}
}
输出效果:
图1
使用ObjectNameAutoProxyCreator经常需要对要拦截的方法进行筛选,这时我用到Spring.Aop.Support.NameMatchMethodPointcutAdvisor,稍微修改一下配置:
App.config
<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"/>
输出效果
![Spring.NET教程(十五)——AOP的配置(基础篇) Level 200](http://img.ddvip.com/2009_11_17/1258420304_ddvip_4059.bmp)
图2
MappedNames的配置为:Find*,因此能够拦截到FindAll方法。
二、正则表达式切入点:RegularExpressionMethodPointcutAdvisor和SdkRegularExpressionMethodPointcut
DefaultAdvisorAutoProxyCreator类会在当前容器中自动应用满足条件的Advisor,而不用在自动代理Advisor的对象定义中包含特定的对象名。它既可以保持配置文件的一致性,又可避免ObjectNameAutoProxyCreator引起的配置文件的臃肿。
先来说RegularExpressionMethodPointcutAdvisor。
App.config
<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
以上配置相对复杂一点。使用SdkRegularExpressionMethodPointcut的配置就相对简单的多,而项目中SdkRegularExpressionMethodPointcut也经常用到。
SdkRegularExpressionMethodPointcut只需要简单的配置一下通知和切入点就完成了。
App.config
<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
pattern属性为拦截表达式。Service.*的意思是,拦截Service命名空间下(包括子空间)的所有类。如果改为Service.*.Find*",意思为拦截Service命名空间下(包括子空间)的所有类以Find开头的方法或Service命名空间下以Find开头的所有类
输出效果:
图5
三、属性切入点:AttributeMatchMethodPointcutAdvisor
Spring.NET框架运行开发人员自定义属性,拦截标注带有特定属性的类中的方法。
Attribute
public class ConsoleDebugAttribute : Attribute
{
}
public class AttributeService : IService
{
[ConsoleDebug]
public IList FindAll()
{
return new ArrayList();
}
public void Save(object entity)
{
Console.WriteLine("保存:" + entity);
}
}
App.config
<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