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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
课程通过实际项目融入常用开发技术架构,讲授风格独特,提供详细上课日志及答疑,赠送配套的项目架构源码注释详细清晰且表达通俗,均能直接在实际项目中应用,正真的物超所值,价格实惠任务作业:综合运用《C#/.Net企业级系统架构设计实战精讲教程》课程所学知识技能设计一个学生成绩管理系统的架构。要求:1.系统基于MVC的三层架构,各层单独建不同的解决方案文件夹。2.采用Model First开发方式,设计架构时只需要设计学生表(TbStudent)和课程表(TbCourse)。学生表必须有的字段是ID、stuName、age;课程表必须有的字段是ID、courseName、content。3.数据访问层采用Entity Framework或NHibernate来实现,必须封装对上述表的增删改查方法。4.必须依赖接口编程,也就是必须要有数据访问层的接口层、业务逻辑层的接口层等接口层。层层之间必须减少依赖,可以通过简单工厂或抽象工厂。5.至少采用简单工厂、抽象工厂、Spring.Net等技术中的2种来减少层与层之间的依赖等。6.封装出DbSession类,让它拥有所有Dal层实例和SaveChanges方法。7.设计出数据访问层及业务逻辑层主要类的T4模板,以便实体增加时自动生成相应的类。8.表现层要设计相关的控制器和视图来验证设计的系统架构代码的正确性,必须含有验证增删改查的方法。9.开发平台一定要是Visual Studio平台,采用C#开发语言,数据库为SQL Server。10.提交整个系统架构的源文件及生成的数据库文件。(注意: 作业需写在CSDN博客中,请把作业链接贴在评论区,老师会定期逐个批改~~)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lzhdim

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值