Spring.NET教程(十五)AOP的配置(基礎篇)
上篇我學習了Spring.net的四種通知型別,AOP的實現方案比較複雜,是通過程式碼實現的。而Spring.NET框架給我們提供了配置的方式來實現AOP的功能。到目前為止,我們已經討論過使用ProxyFactoryObject或其它類似的工廠物件顯式建立AOP代理的方法。如果應用程式需要建立很多AOP代理,比如當需要代理某個服務層的所有物件時,這種方法就會使配置檔案變的相當龐大。為簡化配置過程,Spring.NET提供了“自動代理”的功能,可以根據條件自動建立代理物件,也就是說,可以將多個物件分組以作為要代理的候選物件。自動代理使用起來比較簡單和方便。我仔細分析了一下,提供的幾種配置差異主要在於切入點的方式不同。目前我實現了三種切入點的配置方式。
首先我們先來看一下準備環境。
通知
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"/>
輸出效果
圖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