Spring AOP 实现原理

来源:互联网 发布:mac ftp下载工具 编辑:程序博客网 时间:2024/05/29 16:38

什么是AOP

AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善。OOP引入封装、继承和多态性等概念来建立一种对象层次结构,用以模拟公共行为的一个集合。当我们需要为分散的对象引入公共行为的时候,OOP则显得无能为力。也就是说,OOP允许你定义从上到下的关系,但并不适合定义从左到右的关系。例如日志功能。日志代码往往水平地散布在所有对象层次中,而与它所散布到的对象的核心功能毫无关系。对于其他类型的代码,如安全性、异常处理和透明的持续性也是如此。这种散布在各处的无关的代码被称为横切(cross-cutting)代码,在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。

 

而AOP技术则恰恰相反,它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其名为“Aspect”,即方面。所谓“方面”,简单地说,就是将那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可操作性和可维护性。AOP代表的是一个横向的关系,如果说“对象”是一个空心的圆柱体,其中封装的是对象的属性和行为;那么面向方面编程的方法,就仿佛一把利刃,将这些空心圆柱体剖开,以获得其内部的消息。而剖开的切面,也就是所谓的“方面”了。然后它又以巧夺天功的妙手将这些剖开的切面复原,不留痕迹。

 

使用“横切”技术,AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处都基本相似。比如权限认证、日志、事务处理。Aop 的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。正如Avanade公司的高级方案构架师Adam Magee所说,AOP的核心思想就是“将应用程序中的商业逻辑同对其提供支持的通用服务进行分离。”

 

实现AOP的技术,主要分为两大类:一是采用动态代理技术,利用截取消息的方式,对该消息进行装饰,以取代原有对象行为的执行;二是采用静态织入的方式,引入特定的语法创建“方面”,从而使得编译器可以在编译期间织入有关“方面”的代码。


AOP使用场景

AOP用来封装横切关注点,具体可以在下面的场景中使用:

 

Authentication 权限

Caching 缓存

Context passing 内容传递

Error handling 错误处理

Lazy loading 懒加载

Debugging  调试

logging, tracing, profiling and monitoring 记录跟踪 优化 校准

Performance optimization 性能优化

Persistence  持久化

Resource pooling 资源池

Synchronization 同步

Transactions 事务


AOP相关概念

方面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象。事务管理是J2EE应用中一个很好的横切关注点例子。方面用Spring的 Advisor或拦截器实现。

 

连接点(Joinpoint): 程序执行过程中明确的点,如方法的调用或特定的异常被抛出。

 

通知(Advice): 在特定的连接点,AOP框架执行的动作。各种类型的通知包括“around”、“before”和“throws”通知。通知类型将在下面讨论。许多AOP框架包括Spring都是以拦截器做通知模型,维护一个“围绕”连接点的拦截器链。Spring中定义了四个advice: BeforeAdvice, AfterAdvice, ThrowAdvice和DynamicIntroductionAdvice

 

切入点(Pointcut): 指定一个通知将被引发的一系列连接点的集合。AOP框架必须允许开发者指定切入点:例如,使用正则表达式。 Spring定义了Pointcut接口,用来组合MethodMatcher和ClassFilter,可以通过名字很清楚的理解, MethodMatcher是用来检查目标类的方法是否可以被应用此通知,而ClassFilter是用来检查Pointcut是否应该应用到目标类上

 

引入(Introduction): 添加方法或字段到被通知的类。 Spring允许引入新的接口到任何被通知的对象。例如,你可以使用一个引入使任何对象实现 IsModified接口,来简化缓存。Spring中要使用Introduction, 可有通过DelegatingIntroductionInterceptor来实现通知,通过DefaultIntroductionAdvisor来配置Advice和代理类要实现的接口

 

目标对象(Target Object): 包含连接点的对象。也被称作被通知或被代理对象。POJO

 

AOP代理(AOP Proxy): AOP框架创建的对象,包含通知。 在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。

 

织入(Weaving): 组装方面来创建一个被通知对象。这可以在编译时完成(例如使用AspectJ编译器),也可以在运行时完成。Spring和其他纯Java AOP框架一样,在运行时完成织入。

组织关系图:

织入器通过在切面中pointcut(切入点定义)来搜索目标(被代理类)的JoinPoint(切入点),然后把要切入的逻辑(Advice)织入到目标对象里,生成代理类。 

两种代理

Spring AOP是基于代理机制的,Spring AOP通过JDK ProxyCGLIB Proxy两种方法实现代理。

  • 如果target object没有实现任何接口,那么Spring将使用CGLIB来实现代理。CGLIB是一个开源项目,它是一个强大的,高性能,高质量的Code生成类库,它可以在运行期扩展Java类与实现Java接口。
  • 如果target object实现了一个以上的接口,那么Spring将使用JDK Proxy来实现代理,因为Spring默认使用的就是JDK Proxy,并且JDK Proxy是基于接口的。这也是Spring提倡的面向接口编程。当然,你也可以强制使用CGLIB来进行代理,但是这样可能会造成性能上的下降。



Spring AOP组件

下面这种类图列出了Spring中主要的AOP组件



如何使用Spring AOP


可以通过配置文件或者编程的方式来使用Spring AOP。

 

配置可以通过xml文件来进行,大概有四种方式:

1.        配置ProxyFactoryBean,显式地设置advisors, advice, target等

2.        配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从容器中获得的其实已经是代理对象

3.        通过<aop:config>来配置

4.        通过<aop: aspectj-autoproxy>来配置,使用AspectJ的注解来标识通知及切入点

 

也可以直接使用ProxyFactory来以编程的方式使用Spring AOP,通过ProxyFactory提供的方法可以设置target对象, advisor等相关配置,最终通过 getProxy()方法来获取代理对象

 

具体使用的示例可以google. 这里略去


Spring AOP代理对象的生成


Spring提供了两种方式来生成代理对象: JDKProxy和Cglib,具体使用哪种方式生成由AopProxyFactory根据AdvisedSupport对象的配置来决定。默认的策略是如果目标类是接口,则使用JDK动态代理技术,否则使用Cglib来生成代理。下面我们来研究一下Spring如何使用JDK来生成代理对象,具体的生成代码放在JdkDynamicAopProxy这个类中,直接上相关代码:


[java] view plain copy
  1. /** 
  2.     * <ol> 
  3.     * <li>获取代理类要实现的接口,除了Advised对象中配置的,还会加上SpringProxy, Advised(opaque=false) 
  4.     * <li>检查上面得到的接口中有没有定义 equals或者hashcode的接口 
  5.     * <li>调用Proxy.newProxyInstance创建代理对象 
  6.     * </ol> 
  7.     */  
  8.    public Object getProxy(ClassLoader classLoader) {  
  9.        if (logger.isDebugEnabled()) {  
  10.            logger.debug("Creating JDK dynamic proxy: target source is " +this.advised.getTargetSource());  
  11.        }  
  12.        Class[] proxiedInterfaces =AopProxyUtils.completeProxiedInterfaces(this.advised);  
  13.        findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);  
  14.        return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);  
  15. }  


 

那这个其实很明了,注释上我也已经写清楚了,不再赘述。

 

下面的问题是,代理对象生成了,那切面是如何织入的?

我们知道InvocationHandler是JDK动态代理的核心,生成的代理对象的方法调用都会委托到InvocationHandler.invoke()方法。而通过JdkDynamicAopProxy的签名我们可以看到这个类其实也实现了InvocationHandler,下面我们就通过分析这个类中实现的invoke()方法来具体看下Spring AOP是如何织入切面的。

 

[java] view plain copy
  1. publicObject invoke(Object proxy, Method method, Object[] args) throwsThrowable {  
  2.        MethodInvocation invocation = null;  
  3.        Object oldProxy = null;  
  4.        boolean setProxyContext = false;  
  5.    
  6.        TargetSource targetSource = this.advised.targetSource;  
  7.        Class targetClass = null;  
  8.        Object target = null;  
  9.    
  10.        try {  
  11.            //eqauls()方法,具目标对象未实现此方法  
  12.            if (!this.equalsDefined && AopUtils.isEqualsMethod(method)){  
  13.                 return (equals(args[0])? Boolean.TRUE : Boolean.FALSE);  
  14.            }  
  15.    
  16.            //hashCode()方法,具目标对象未实现此方法  
  17.            if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)){  
  18.                 return newInteger(hashCode());  
  19.            }  
  20.    
  21.            //Advised接口或者其父接口中定义的方法,直接反射调用,不应用通知  
  22.            if (!this.advised.opaque &&method.getDeclaringClass().isInterface()  
  23.                     &&method.getDeclaringClass().isAssignableFrom(Advised.class)) {  
  24.                 // Service invocations onProxyConfig with the proxy config...  
  25.                 return AopUtils.invokeJoinpointUsingReflection(this.advised,method, args);  
  26.            }  
  27.    
  28.            Object retVal = null;  
  29.    
  30.            if (this.advised.exposeProxy) {  
  31.                 // Make invocation available ifnecessary.  
  32.                 oldProxy = AopContext.setCurrentProxy(proxy);  
  33.                 setProxyContext = true;  
  34.            }  
  35.    
  36.            //获得目标对象的类  
  37.            target = targetSource.getTarget();  
  38.            if (target != null) {  
  39.                 targetClass = target.getClass();  
  40.            }  
  41.    
  42.            //获取可以应用到此方法上的Interceptor列表  
  43.            List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method,targetClass);  
  44.    
  45.            //如果没有可以应用到此方法的通知(Interceptor),此直接反射调用 method.invoke(target, args)  
  46.            if (chain.isEmpty()) {  
  47.                 retVal = AopUtils.invokeJoinpointUsingReflection(target,method, args);  
  48.            } else {  
  49.                 //创建MethodInvocation  
  50.                 invocation = newReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);  
  51.                 retVal = invocation.proceed();  
  52.            }  
  53.    
  54.            // Massage return value if necessary.  
  55.            if (retVal != null && retVal == target &&method.getReturnType().isInstance(proxy)  
  56.                     &&!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {  
  57.                 // Special case: it returned"this" and the return type of the method  
  58.                 // is type-compatible. Notethat we can't help if the target sets  
  59.                 // a reference to itself inanother returned object.  
  60.                 retVal = proxy;  
  61.            }  
  62.            return retVal;  
  63.        } finally {  
  64.            if (target != null && !targetSource.isStatic()) {  
  65.                 // Must have come fromTargetSource.  
  66.                targetSource.releaseTarget(target);  
  67.            }  
  68.            if (setProxyContext) {  
  69.                 // Restore old proxy.  
  70.                 AopContext.setCurrentProxy(oldProxy);  
  71.            }  
  72.        }  
  73.     }  



 

主流程可以简述为:获取可以应用到此方法上的通知链(Interceptor Chain),如果有,则应用通知,并执行joinpoint; 如果没有,则直接反射执行joinpoint。而这里的关键是通知链是如何获取的以及它又是如何执行的,下面逐一分析下。

 

首先,从上面的代码可以看到,通知链是通过Advised.getInterceptorsAndDynamicInterceptionAdvice()这个方法来获取的,我们来看下这个方法的实现:

[java] view plain copy
  1. public List<Object>getInterceptorsAndDynamicInterceptionAdvice(Method method, Class targetClass) {  
  2.                    MethodCacheKeycacheKey = new MethodCacheKey(method);  
  3.                    List<Object>cached = this.methodCache.get(cacheKey);  
  4.                    if(cached == null) {  
  5.                             cached= this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(  
  6.                                                this,method, targetClass);  
  7.                             this.methodCache.put(cacheKey,cached);  
  8.                    }  
  9.                    returncached;  
  10.          }  


 

可以看到实际的获取工作其实是由AdvisorChainFactory. getInterceptorsAndDynamicInterceptionAdvice()这个方法来完成的,获取到的结果会被缓存。

下面来分析下这个方法的实现:

 

[java] view plain copy
  1. /** 
  2.     * 从提供的配置实例config中获取advisor列表,遍历处理这些advisor.如果是IntroductionAdvisor, 
  3.     * 则判断此Advisor能否应用到目标类targetClass上.如果是PointcutAdvisor,则判断 
  4.     * 此Advisor能否应用到目标方法method上.将满足条件的Advisor通过AdvisorAdaptor转化成Interceptor列表返回. 
  5.     */  
  6.     publicList getInterceptorsAndDynamicInterceptionAdvice(Advised config, Methodmethod, Class targetClass) {  
  7.        // This is somewhat tricky... we have to process introductions first,  
  8.        // but we need to preserve order in the ultimate list.  
  9.        List interceptorList = new ArrayList(config.getAdvisors().length);  
  10.    
  11.        //查看是否包含IntroductionAdvisor  
  12.        boolean hasIntroductions = hasMatchingIntroductions(config,targetClass);  
  13.    
  14.        //这里实际上注册一系列AdvisorAdapter,用于将Advisor转化成MethodInterceptor  
  15.        AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();  
  16.    
  17.        Advisor[] advisors = config.getAdvisors();  
  18.         for (int i = 0; i <advisors.length; i++) {  
  19.            Advisor advisor = advisors[i];  
  20.            if (advisor instanceof PointcutAdvisor) {  
  21.                 // Add it conditionally.  
  22.                 PointcutAdvisor pointcutAdvisor= (PointcutAdvisor) advisor;  
  23.                 if(config.isPreFiltered() ||pointcutAdvisor.getPointcut().getClassFilter().matches(targetClass)) {  
  24.                     //TODO: 这个地方这两个方法的位置可以互换下  
  25.                     //将Advisor转化成Interceptor  
  26.                     MethodInterceptor[]interceptors = registry.getInterceptors(advisor);  
  27.    
  28.                     //检查当前advisor的pointcut是否可以匹配当前方法  
  29.                     MethodMatcher mm =pointcutAdvisor.getPointcut().getMethodMatcher();  
  30.    
  31.                     if (MethodMatchers.matches(mm,method, targetClass, hasIntroductions)) {  
  32.                         if(mm.isRuntime()) {  
  33.                             // Creating a newobject instance in the getInterceptors() method  
  34.                             // isn't a problemas we normally cache created chains.  
  35.                             for (intj = 0; j < interceptors.length; j++) {  
  36.                                interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptors[j],mm));  
  37.                             }  
  38.                         } else {  
  39.                             interceptorList.addAll(Arrays.asList(interceptors));  
  40.                         }  
  41.                     }  
  42.                 }  
  43.            } else if (advisor instanceof IntroductionAdvisor){  
  44.                 IntroductionAdvisor ia =(IntroductionAdvisor) advisor;  
  45.                 if(config.isPreFiltered() || ia.getClassFilter().matches(targetClass)) {  
  46.                     Interceptor[] interceptors= registry.getInterceptors(advisor);  
  47.                     interceptorList.addAll(Arrays.asList(interceptors));  
  48.                 }  
  49.            } else {  
  50.                 Interceptor[] interceptors =registry.getInterceptors(advisor);  
  51.                 interceptorList.addAll(Arrays.asList(interceptors));  
  52.            }  
  53.        }  
  54.        return interceptorList;  
  55. }  


 

这个方法执行完成后,Advised中配置能够应用到连接点或者目标类的Advisor全部被转化成了MethodInterceptor.

 

接下来我们再看下得到的拦截器链是怎么起作用的。

 

[java] view plain copy
  1. if (chain.isEmpty()) {  
  2.                 retVal = AopUtils.invokeJoinpointUsingReflection(target,method, args);  
  3.             } else {  
  4.                 //创建MethodInvocation  
  5.                 invocation = newReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);  
  6.                 retVal = invocation.proceed();  
  7.             }  


        

         从这段代码可以看出,如果得到的拦截器链为空,则直接反射调用目标方法,否则创建MethodInvocation,调用其proceed方法,触发拦截器链的执行,来看下具体代码

[java] view plain copy
  1. public Object proceed() throws Throwable {  
  2.        //  We start with an index of -1and increment early.  
  3.        if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size()- 1) {  
  4.            //如果Interceptor执行完了,则执行joinPoint  
  5.            return invokeJoinpoint();  
  6.        }  
  7.    
  8.        Object interceptorOrInterceptionAdvice =  
  9.            this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);  
  10.          
  11.        //如果要动态匹配joinPoint  
  12.        if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher){  
  13.            // Evaluate dynamic method matcher here: static part will already have  
  14.            // been evaluated and found to match.  
  15.            InterceptorAndDynamicMethodMatcher dm =  
  16.                 (InterceptorAndDynamicMethodMatcher)interceptorOrInterceptionAdvice;  
  17.            //动态匹配:运行时参数是否满足匹配条件  
  18.            if (dm.methodMatcher.matches(this.method, this.targetClass,this.arguments)) {  
  19.                 //执行当前Intercetpor  
  20.                 returndm.interceptor.invoke(this);  
  21.            }  
  22.            else {  
  23.                 //动态匹配失败时,略过当前Intercetpor,调用下一个Interceptor  
  24.                 return proceed();  
  25.            }  
  26.        }  
  27.        else {  
  28.            // It's an interceptor, so we just invoke it: The pointcutwill have  
  29.            // been evaluated statically before this object was constructed.  
  30.            //执行当前Intercetpor  
  31.            return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);  
  32.        }  
  33. }  


代码也比较简单,这里不再赘述。



END


Spring AOP的应用

我们可以通过三种方式来使用Spring AOP,它们分别是:@Aspect-based(Annotation)Schema-based(XML)以及底层的Spring AOP API

一、@Aspect-based (Annotation)

通过Annotaion(注解)实现AOP是最常用的方式。

1)配置

        首先,我们应该在配置文件中增加对Annotation的支持。

        假设我们的配置文件是classpath下的applicationContext.xml,添加如下片段:

 <!-- 添加对 @AspectJ 注解的支持 -->           <aop:aspectj-autoproxy />       <!-- 业务类定义 -->      <bean id="customerService" class="com.tech.aop.service.impl.CustomerServiceBean" />            <!-- 切面(Aspect)类定义 -->      <bean id="myInterceptor" class="com.tech.xml.aop.MyInterceptor"/> 

2)业务逻辑类

       假设我们有一个UserManager类,这个类负责处理业务逻辑。类的定义如下:

public class UserManager {/*这个方法需要一个参数*/public void addUser(String user) {      System.out.println("addUser(String str) method is executed!");}public void deleteUser() {      System.out.println("deleteUser() method is executed!");}/*这个方法返回一个字符串*/public String getUser() {      System.out.println("getUser() method is executed!");      return "Hello";}/*这个方法抛出一个异常*/public void editUser() throws Exception {      throw new Exception("something is wrong.");} }

这是一个很普通的Java对象,看不出任何Spring AOP的痕迹,这也是Spring低侵入式设计的体现。

3)切面(Aspect)类

为了给业务逻辑增加额外功能,我们需要定义一个切面类,切面类里包含了pointcut和advice。假设我们的切面类是ExampleAspect,代码如下:

@Aspectpublic class ExampleAspect {@Pointcut("execution(* com.psjay.example.spring.aop.*.*(..))")public void aPointcut() {}@Before("aPointcut()")public void beforeAdvice() {System.out.println("before advice is executed!");}@AfterReturning(pointcut = "aPointcut()", returning = "r")public void afterReturningAdvice(String r) {if (r != null)System.out.println("after returning advice is executed! returning String is : "+ r);}@After("aPointcut()")public void AfterAdvice() {System.out.println("after advice is executed!");}@After("aPointcut() && args(str)")public void AfterAdviceWithArg(String str) {System.out.println("after advice with arg is executed!arg is : " + str);}@AfterThrowing(pointcut = "aPointcut()", throwing = "e")public void afterThrowingAdvice(Exception e) {System.out.println("after throwing advice is executed!exception msg is : "+ e.getMessage());}}

在基于annotation的Spring AOP中,@Aspect用来标注切面类。@Pointcut标注一个空的方法,用来代表一个pointcut,这个方法必须是public的。@Pointcut注解括号内是pointcut expression,例子中的表达式表示com.psjay.example.spring.aop的所有方法都是join point。而@Before,@After等注解对应着几种不同类型的Advice。被标注的方法就是一个Advice。@Advice注解括号内是一个pointcut。例子中的@afterReturningAdvice(),AfterAdviceWithArg()和afterThrowingAdvice()分别演示了Advice得到join point的返回值,Advice使用join point的参数,Advice使用join point抛出的异常对象几种操作。

不要忘了在Spring配置文件中配置以上两个类的“Bean”,这里就不贴出具体代码了。

4)测试类

测试类相对简单,就是从Spring中拿出bean演示AOP的结果。测试类代码如下:

public class Test {    public static void main(String[] args) {        ApplicationContext ctx = new ClassPathXmlApplicationContext(                "applicationContext.xml");        UserManager um = ctx.getBean("userManager", UserManager.class);        System.out.println("------ Case 1 --------");        um.addUser("hey");        System.out.println("------ Case 2 --------");        try {            um.editUser();        } catch (Exception e) {        }        System.out.println("------ Case 3 --------");        um.getUser();    }}

测试结果:

—— Case 1 ——–before advice is executed!addUser(String str) method is executed!after advice is executed!after advice with arg is executed!arg is : hey—— Case 2 ——–before advice is executed!after advice is executed!after throwing advice is executed!exception msg is : something is wrong.—— Case 3 ——–before advice is executed!getUser() method is executed!after returning advice is executed! returning String is : Helloafter advice is executed!

可以看到,Advice已经在对应的join point上起作用了。

 

二、 Schema-based(XML)

除了使用Annotation,我们还可以使用XML来实现Spring AOP。使用XML来实现AOP只是将AOP的配置信息移到XML配置文件里。

1)业务类

package com.tech.aop.service;public interface CustomerService {public String getName(Integer id);public void save(String name);public void update(Integer id, String name);}


package com.tech.aop.service.impl;import com.tech.aop.service.CustomerService;public class CustomerServiceBean implements CustomerService {@Overridepublic String getName(Integer id) {System.out.println("这是find方法");return "zhang";}@Overridepublic void save(String name) {//throw new RuntimeException("例外通知");System.out.println("这是save方法");}@Overridepublic void update(Integer personId, String name) {System.out.println("这是update方法");}}

2)切面(Aspect)类

package com.tech.xml.aop;import org.aspectj.lang.ProceedingJoinPoint;/** * 切面 *  * @author ch *  */public class MyInterceptor {public void doBefore() {System.out.println("前置通知");}public void doAfterReturning() {System.out.println("后置通知");}public void doAfter() {System.out.println("最终通知");}public void doAfterThrowing() {System.out.println("例外通知");}public Object doBasicProfiling(ProceedingJoinPoint  pjp) throws Throwable {System.out.println("进入方法");Object result = pjp.proceed();System.out.println("退出 方法");return result;}}


3)xml配置aop

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"default-lazy-init="false"><!-- 添加对 @AspectJ 注解的支持 -->     <aop:aspectj-autoproxy /> <!-- 业务类定义 --><bean id="customerService" class="com.tech.aop.service.impl.CustomerServiceBean" /><!-- 切面(Aspect)类定义 --><bean id="myInterceptor" class="com.tech.xml.aop.MyInterceptor"/><!-- AOP配置 --><aop:config>    <!-- 配置切面类 --> <aop:aspect id="apt" ref="myInterceptor"><!-- 定义切入点(通过表达式对指定方法进行拦截) -->    <aop:pointcut id="mypCut" expression="execution(* com.tech.aop.service.impl.CustomerServiceBean.*(..))"/><!-- 定义advice(不同类型的通知) --><aop:before method="doBefore" pointcut-ref="mypCut"/><aop:after-returning method="doAfterReturning" pointcut-ref="mypCut"/><aop:after-throwing method="doAfterThrowing" pointcut-ref="mypCut"/><aop:after method="doAfter" pointcut-ref="mypCut"/><aop:around method="doBasicProfiling" pointcut-ref="mypCut"/></aop:aspect></aop:config></beans>

4)测试

package com.tech.xml.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.tech.aop.service.CustomerService;public class TestXmlAop {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext ac = new  ClassPathXmlApplicationContext("applicationContext.xml");CustomerService service = (CustomerService)ac.getBean("customerService");service.save("abc");}}

输出结果:

前置通知进入方法这是save方法后置通知最终通知退出 方法

Spring AOP API

在Spring1.2中使用底层的Spring AOP API来实现AOP。当然,Spring3也是完全与其兼容的。我们可以借其窥探一下底层实现。在此不做介绍。

需要的jar包


总结

Spring AOP是基于代理的,是运行时绑定的。合理的运用AOP,将使软件的开发更加便捷,清晰。

Spring AOP的应用主要有以下几个方面:

  • 性能监控,在方法调用前后记录调用时间,方法执行太长或超时报警。
  • 工作流系统,工作流系统需要将业务代码和流程引擎代码混合在一起执行,那么我们可以使用AOP将其分离,并动态挂接业务。
  • 权限验证,方法执行前验证是否有权限执行当前方法,没有则抛出没有权限执行异常,由业务代码捕捉。
  • 缓存代理,缓存某方法的返回值,下次执行该方法时,直接从缓存里获取。
  • 软件破解,使用AOP修改软件的验证类的判断逻辑。
  • 记录日志,在方法执行前后记录系统日志。

Spring AOP 相关概念

此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题。最近项目中遇到了以下几点需求,仔细思考之后,觉得采用AOP来解决。一方面是为了以更加灵活的方式来解决问题,另一方面是借此机会深入学习SpringAOP相关的内容。本文是权当本人的自己AOP学习笔记,以下需求不用AOP肯定也能解决,至于是否牵强附会,仁者见仁智者见智。

  1. 对部分函数的调用进行日志记录,用于观察特定问题在运行过程中的函数调用情况
  2. 监控部分重要函数,若抛出指定的异常,需要以短信或邮件方式通知相关人员
  3. 金控部分重要函数的执行时间

    事实上,以上需求没有AOP也能搞定,只是在实现过程中比较郁闷摆了。

  1. 需要打印日志的函数分散在各个包中,只能找到所有的函数体,手动添加日志。然而这些日志都是临时的,待问题解决之后应该需要清除打印日志的代码,只能再次手动清除!
  2. 类似1的情况,需要捕获异常的地方太多,如果手动添加时想到很可能明天又要手动清除,只能再汗。OK,该需求相对比较固定,属于长期监控的范畴,并不需求临时添加后再清除。然而,客户某天要求,把其中20%的异常改为短信提醒,剩下的80%改用邮件提醒。改之,两天后,客户抱怨短信太多,全部改成邮件提醒...
  3. 该需求通常用于监控某些函数的执行时间,用以判断系统执行慢的瓶颈所在。瓶颈被解决之后,烦恼同情况1


    终于下定决心,采用AOP来解决!代码如下:

package com.spring.aop;/** * 切面 * */public class TestAspect {    public void doAfter(JoinPoint jp) {        System.out.println("log Ending method: "                + jp.getTarget().getClass().getName() + "."                + jp.getSignature().getName());    }    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {        long time = System.currentTimeMillis();        Object retVal = pjp.proceed();        time = System.currentTimeMillis() - time;        System.out.println("process time: " + time + " ms");        return retVal;    }    public void doBefore(JoinPoint jp) {        System.out.println("log Begining method: "                + jp.getTarget().getClass().getName() + "."                + jp.getSignature().getName());    }    public void doThrowing(JoinPoint jp, Throwable ex) {        System.out.println("method " + jp.getTarget().getClass().getName()                + "." + jp.getSignature().getName() + " throw exception");        System.out.println(ex.getMessage());    }    private void sendEx(String ex) {        //TODO 发送短信或邮件提醒    }} 


package com.spring.service;/** * 接口A */public interface AService {        public void fooA(String _msg);    public void barA();}

package com.spring.service;/** *接口A的实现类 */public class AServiceImpl implements AService {    public void barA() {        System.out.println("AServiceImpl.barA()");    }    public void fooA(String _msg) {        System.out.println("AServiceImpl.fooA(msg:"+_msg+")");    }}

package com.spring.service;/** *   Service类B */public class BServiceImpl {    public void barB(String _msg, int _type) {        System.out.println("BServiceImpl.barB(msg:"+_msg+" type:"+_type+")");        if(_type == 1)            throw new IllegalArgumentException("测试异常");    }    public void fooB() {        System.out.println("BServiceImpl.fooB()");    }}


<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="            http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-2.0.xsd            http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"    default-autowire="autodetect">    <aop:config>        <aop:aspect id="TestAspect" ref="aspectBean">            <!--配置com.spring.service包下所有类或接口的所有方法-->            <aop:pointcut id="businessService"                expression="execution(* com.spring.service.*.*(..))" />            <aop:before pointcut-ref="businessService" method="doBefore"/>            <aop:after pointcut-ref="businessService" method="doAfter"/>            <aop:around pointcut-ref="businessService" method="doAround"/>            <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>        </aop:aspect>    </aop:config>        <bean id="aspectBean" class="com.spring.aop.TestAspect" />    <bean id="aService" class="com.spring.service.AServiceImpl"></bean>    <bean id="bService" class="com.spring.service.BServiceImpl"></bean></beans>

测试类AOPTest

public class AOPTest extends AbstractDependencyInjectionSpringContextTests {private AService aService;private BServiceImpl bService;protected String[] getConfigLocations() {String[] configs = new String[] { "/applicationContext.xml"};return configs;}/** * 测试正常调用 */public void testCall(){System.out.println("SpringTest JUnit test");aService.fooA("JUnit test fooA");aService.barA();bService.fooB();bService.barB("JUnit test barB",0);}/** * 测试After-Throwing */public void testThrow(){try {bService.barB("JUnit call barB",1);} catch (IllegalArgumentException e) {}}public void setAService(AService service) {aService = service;}public void setBService(BServiceImpl service) {bService = service;}}

运行结果如下:

log Begining method: com.spring.service.AServiceImpl.fooAAServiceImpl.fooA(msg:JUnit test fooA)log Ending method: com.spring.service.AServiceImpl.fooAprocess time: 0 mslog Begining method: com.spring.service.AServiceImpl.barAAServiceImpl.barA()log Ending method: com.spring.service.AServiceImpl.barAprocess time: 0 mslog Begining method: com.spring.service.BServiceImpl.fooBBServiceImpl.fooB()log Ending method: com.spring.service.BServiceImpl.fooBprocess time: 0 mslog Begining method: com.spring.service.BServiceImpl.barBBServiceImpl.barB(msg:JUnit test barB type:0)log Ending method: com.spring.service.BServiceImpl.barBprocess time: 0 mslog Begining method: com.spring.service.BServiceImpl.barBBServiceImpl.barB(msg:JUnit call barB type:1)log Ending method: com.spring.service.BServiceImpl.barBmethod com.spring.service.BServiceImpl.barB throw exception测试异常

《Spring参考手册》中定义了以下几个AOP的重要概念,结合以上代码分析如下:

  • 切面(Aspect):官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如,AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置。
  • 连接点(Joinpoint):程序执行过程中的某一行为,例如,AServiceImpl.barA()的调用或者BServiceImpl.barB(String _msg, int _type)抛出异常等行为。
  • 通知(Advice):“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如TestAspect
  • 切入点(Pointcut):匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.*.*(..))来决定
  • 目标对象(Target Object):被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
  • AOP代理(AOP Proxy)在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将<aop:config>proxy-target-class 属性设为true

       通知(Advice)类型

  • 前置通知(Before advice):在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。例如,TestAspect中的doBefore方法
  • 后通知(After advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。例如,TestAspect中的doAfter方法,所以AOPTest中调用BServiceImpl.barB抛出异常时,doAfter方法仍然执行
  • 返回后通知(After return advice):在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素进行声明。
  • 环绕通知(Around advice):包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。例如,TestAspect中的doAround方法。
  • 抛出异常后通知(After throwing advice): 在方法抛出异常退出时执行的通知。 ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。例如,TestAspect中的doThrowing方法。

       切入点表达式

  • 通常情况下,表达式中使用”execution“就可以满足大部分的要求。表达式格式如下:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)

modifiers-pattern:方法的操作权限

ret-type-pattern:返回值

declaring-type-pattern:方法所在的包

name-pattern:方法名

parm-pattern:参数名

throws-pattern:异常

其中,除ret-type-pattern和name-pattern之外,其他都是可选的。上例中,execution(* com.spring.service.*.*(..))表示com.spring.service包下,返回值为任意类型;方法名任意;参数不作限制的所有方法。

  • 通知参数

可以通过args来绑定参数,这样就可以在通知(Advice)中访问具体参数了。例如,<aop:aspect>配置如下:

<aop:config><aop:aspect id="TestAspect" ref="aspectBean"><aop:pointcut id="businessService"expression="execution(* com.spring.service.*.*(String,..)) and args(msg,..)" /><aop:after pointcut-ref="businessService" method="doAfter"/></aop:aspect></aop:config>

TestAspect的doAfter方法中就可以访问msg参数,但这样以来AService中的barA()和BServiceImpl中的barB()就不再是连接点,因为execution(* com.spring.service.*.*(String,..))只配置第一个参数为String类型的方法。其中,doAfter方法定义如下:

public void doAfter(JoinPoint jp,String msg)

  •   访问当前的连接点

任何通知(Advice)方法可以将第一个参数定义为 org.aspectj.lang.JoinPoint 类型。JoinPoint 接口提供了一系列有用的方法, 比如getArgs()(返回方法参数)、getThis()(返回代理对象)、getTarget()(返回目标)、getSignature()(返回正在被通知的方法相关信息)和toString()(打印出正在被通知的方法的有用信息。




参考文章:http://blog.csdn.net/moreevan/article/details/11977115

                 http://blog.csdn.net/xiaohai0504/article/details/6880991


0 0
原创粉丝点击