对Spring.Net的AOP一些思考及应用
我们无法通过抽象父类消除上面说的 重复性代码,因为这些代码依附在业务逻辑中了。AOP独辟蹊径,不通过纵向架构,而是横向的看代码,把这些横向的逻辑代码抽取出来,做成一个独立的模块。


参考
SimpleCompanyManager 这个类依赖了
SecurityManager,发生了业务性的耦合。我们用代理模式来实现看看会发生什么。
SecurityManager耦合,相当于是加了一层,但这样实现起来比较麻烦这样,
public class AroundAdvice : IMethodInterceptor
MethodInterceptor这个接口(JDK的动态代理接口为
InvocationHandler,在
java.lang.reflect这个包里面)。.NET里的spring只是少了一个I!如此看来语言是相通的,特别对于统一框架而言:D。 (
其实实际的项目经常用的是@Aspectj注解,支持正则表达式,用起来更加方便)
CGLIB是一个强大的高性能的
代码生成
包。它广泛的被许多AOP的
框架
使用,例如Spring AOP和dynaop,为他们提供方法的interception(拦截)。
ProxyFactory,然后加入
Advice(通知,感觉叫增强更合适),最后使用
GetProxy获取代理,执行方法。
AOP的通知类型
AOP代理,比如当需要代理某个服务层的所有对象时,这种方法就会使配置文件变的相当庞大。为简化配置过程,Spring.NET提供了“
自动代理”的功能,可以根据条件自动创建
代理对象,也就是说,可以将多个对象分组以作为要代理的候选对象。
自动代理使用起来比较简单和方便.
<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"/>
<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>
<
objec
t id="aroundAdvice" type="Common.AroundAdvice, Common"/>
<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"/>
以上配置相对复杂一点。使用SdkRegularExpressionMethodPointcut的配置就相对简单的多,而项目中SdkRegularExpressionMethodPointcut也经常用到。
SdkRegularExpressionMethodPointcut只需要简单的配置一下通知和切入点就完成了。
- <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"/>
- 是不是和JAVA的Spring XML配置完全一样?:D
- 3.属性切入点
- public
- class ConsoleDebugAttribute : Attribute
- {
- }
- public
- class AttributeService : IService
- {
- [ConsoleDebug]
- public IList FindAll()
- {
- return
- new ArrayList();
- }
- public
- void Save(object entity)
- {
- Console.WriteLine("保存:"
- + entity);
- }
- }
- JAVA版的是注解,下面是配置文件
- <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"/>
- 完
- 以上所有源码下载
对Spring.Net的AOP一些思考及应用的更多相关文章
- 【转】spring - ioc和aop
[转]spring - ioc和aop 1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对 ...
- Spring中的AOP
什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...
- Spring Boot2(六):使用Spring Boot整合AOP面向切面编程
一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop aop全称Aspec ...
- Spring 3.0 AOP (一)AOP 术语
关于AOP.之前我已写过一个系列的随笔: <自己实现简单的AOP>,它的关注点在于实现.实现语言是C#,实现方式为 自定义实现 RealProxy 抽象类.重写Invoke方法,以便进行方 ...
- Spring系列之AOP实现的两种方式
AOP常用的实现方式有两种,一种是采用声明的方式来实现(基于XML),一种是采用注解的方式来实现(基于AspectJ). 首先复习下AOP中一些比较重要的概念: Joinpoint(连接点):程序执行 ...
- Spring MVC 线程安全问题的思考
Spring MVC 线程安全问题的思考 在读一些博文的时候发现有些文章对SpringMVC的Controller线程安全的验证并不正确,比如没有探究controller线程不安全的具体原因,比如将请 ...
- springMVC+MyBatis+Spring 整合(4) ---解决Spring MVC 对AOP不起作用的问题
解决Spring MVC 对AOP不起作用的问题 分类: SpringMVC3x+Spring3x+MyBatis3x myibaits spring J2EE2013-11-21 11:22 640 ...
- Spring核心框架 - AOP的原理及源码解析
一.AOP的体系结构 如下图所示:(引自AOP联盟) 层次3语言和开发环境:基础是指待增加对象或者目标对象:切面通常包括对于基础的增加应用:配置是指AOP体系中提供的配置环境或者编织配置,通过该配置A ...
- Spring IOC及AOP学习总结
一.Spring IOC体系学习总结: Spring中有两个容器体系,一类是BeanFactory.还有一类是ApplicationContext.BeanFactory提供了基础的容器功能.Appl ...
随机推荐
- fmri的图像数据在matlab中显示,利用imagesc工具进行显示,自带数据集-by 西南大学xulei教授
这里包含了这样一个数据集:slice_data.mat. 这个数据集中包含的mri数据是:64*64*25.共有25个slice.每个slice的分辨率是64*64. 程序非常简短: load sli ...
- Java中String为什么是final
final概念: 如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父亲被继承.因此,一个类不能既被声明为abstract,又被声明为final. 将变量或方法声明为final,可以 ...
- dynamic_cast
作为四个内部类型转换操作符之一的dynamic_cast和传统的C风格的强制类型转换有着巨大的差别.除了dynamic_cast以外的转换,其行为的都是在编译期就得以确定的,转换是否成功,并不依赖被转 ...
- Linux下的Memcache安装
Linux下Memcache服务器端的安装服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-1.3.0 .下载:http://www.danga.com/memcach ...
- Ext.Loader
Ext.Loader是Ext JS4动态加载的核心,等价于Ext.require简写. Ext.Loader支持异步和同步加载的方法. 异步 优点: 1.跨域 2.不需要web服务器 3.调试方便(可 ...
- cppunit使用详解
cppunit使用详解 第一步:如何安装 (我的运行环境: fc7 Linux, gcc4) cppunit 的安装是相当标准的linux的安装过程 a. 下载cppunit的源文件 ...
- 关于div居中
margin : 100px; margin-left: auto; margin-right: auto; 这样子设置css样式就可以实现一个div居中
- [Papers]NSE, $u$, Lorentz space [Bjorland-Vasseur, JMFM, 2011]
$$\bex \int_0^T\frac{\sen{\bbu}_{L^{q,\infty}}^p}{\ve+\ln \sex{e+\sen{\bbu}_{L^\infty}}}\rd s<\in ...
- JDBC数据源(DataSource)的简单实现
数据源技术是Java操作数据库的一个很关键技术,流行的持久化框架都离不开数据源的应用. 数据源提供了一种简单获取数据库连接的方式,并能在内部通过一个池的机制来复用数据库连接,这样就大大减少创建数据 ...
- Java并发编程-synchronized
多线程的同步机制对资源进行加锁,使得在同一个时间,只有一个线程可以进行操作,同步用以解决多个线程同时访问时可能出现的问题.同步机制可以使用synchronized关键字实现.synchronized关 ...