您的位置:首页 > 产品设计 > UI/UE

Sprinet.NET 学习笔记(2)-AOPQuickStart

2009-06-07 23:59 567 查看
1. Spring.AopQuickStart.Step1.2005 中使用new关键字进行AOP的实现
ProxyFactory factory = newProxyFactory(newServiceCommand());
factory.AddAdvice(newConsoleLoggingBeforeAdvice());
factory.AddAdvice(newConsoleLoggingAfterAdvice());
factory.AddAdvice(newConsoleLoggingThrowsAdvice());
ICommand command = (ICommand)factory.GetProxy();
command.Execute();
if (command.IsUndoCapable)
{
command.UnExecute();
}
2. Spring.AopQuickStart.Step2.2005 App.config配置进行AOP的实现
<spring>

<context>
<resource uri="config://spring/objects" />
</context>

<objects xmlns="http://www.springframework.net">
<description>相关描述</description>
<object id="aroundAdvice" type="Spring.AopQuickStart.Aspects.ConsoleLoggingAroundAdvice, Spring.AopQuickStart.Common" />
<object id="throwsAdvice" type="Spring.AopQuickStart.Aspects.ConsoleLoggingThrowsAdvice, Spring.AopQuickStart.Common" />
<object id="myServiceCommand" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="Target">
<object type="Spring.AopQuickStart.Commands.ServiceCommand, Spring.AopQuickStart.Common" />
</property>
<property name="InterceptorNames">
<list>
<value>aroundAdvice</value>
<value>throwsAdvice</value>
</list>
</property>
</object>
</objects>

</spring>
3. Spring.AopQuickStart.Step3.2005 通过配置NameMatchMethodPointcutAdvisor类使用名为MapperNames的属性进行拦截,从而匹配*Execute的方法所有方法的拦截
<spring>

<context>
<resource uri="config://spring/objects" />
</context>

<objects xmlns="http://www.springframework.net">
<description>相关描述</description>
<object id="aroundAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop">
<property name="Advice">
<object type="Spring.AopQuickStart.Aspects.ConsoleLoggingAroundAdvice, Spring.AopQuickStart.Common" />
</property>
<property name="MappedNames">
<list>
<value>*Execute</value>
</list>
</property>
</object>

<object id="throwsAdvice" type="Spring.AopQuickStart.Aspects.ConsoleLoggingThrowsAdvice, Spring.AopQuickStart.Common" />
<object id="myServiceCommand" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="Target">
<object type="Spring.AopQuickStart.Commands.ServiceCommand, Spring.AopQuickStart.Common" />
</property>
<property name="InterceptorNames">
<list>
<value>aroundAdvisor</value>
<value>throwsAdvice</value>
</list>
</property>
</object>
</objects>

</spring>
4. Spring.AopQuickStart.Step5.2005通过配置NameMatchMethodPointcutAdvisor类使用名为Expression的属性进行拦截,下面为拦截方法返还类型为void,没有的参数的例子
<objects xmlns="http://www.springframework.net">
<description>相关描述</description>
<object id="aroundAdvisor"
type="Spring.AopQuickStart.Aspects.ExpressionDynamicPointcutAdvisor, Spring.AopQuickStart.Step5">
<property name="Advice">
<object type="Spring.AopQuickStart.Aspects.ConsoleLoggingAroundAdvice, Spring.AopQuickStart.Common" />
</property>
<property name="Expression" value="MethodInfo.ReturnType == T(System.Void) and Args != null" />
</object>
<object id="throwsAdvice"
type="Spring.AopQuickStart.Aspects.ConsoleLoggingThrowsAdvice, Spring.AopQuickStart.Common" />
<object id="myServiceCommand" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="Target">
<object type="Spring.AopQuickStart.Commands.ServiceCommand, Spring.AopQuickStart.Common" />
</property>
<property name="InterceptorNames">
<list>
<value>aroundAdvisor</value>
<value>throwsAdvice</value>
</list>
</property>
</object>
</objects>
5. Spring.AopQuickStart.Step5.2005 通过配置ObjectNameAutoProxyCreator类使用ObjectNames属性匹配*ServiceCommand进行拦截
<objects xmlns="http://www.springframework.net">
<description>相关描述</description>
<object id="myServiceCommand"
type="Spring.AopQuickStart.Commands.ServiceCommand, Spring.AopQuickStart.Common" />
<object id="myAnotherServiceCommand"
type="Spring.AopQuickStart.Commands.AnotherServiceCommand, Spring.AopQuickStart.Step7" />
<object id="aroundAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop">
<property name="Advice">
<object type="Spring.AopQuickStart.Aspects.ConsoleLoggingAroundAdvice, Spring.AopQuickStart.Common" />
</property>
<property name="MappedNames">
<list>
<value>*Execute</value>
</list>
</property>
</object>
<object type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop">
<property name="ObjectNames">
<list>
<value>*ServiceCommand</value>
</list>
</property>
<property name="InterceptorNames">
<list>
<value>aroundAdvisor</value>
</list>
</property>
</object>
</objects>

其中QuickStart4和6没有整理好,好好学习之后再整理和大家分享。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: