程序员成长记录

AOP原理

2019-03-12  本文已影响0人  SilentBillows

AOP原理


  AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程,当前已经成为一种比较成熟的编程思想,可以用来很好的解决应用系统中分布于各个模块的交叉关注点问题。在轻量级的J2EE中应用开发中,使用AOP来灵活处理一些具有横切性质的系统级服务,如事务处理、安全检查、缓存、对象池管理等,已经成为一种非常适用的解决方案。 AOP中比较重要的概念有:Aspect、JoinPoint、PonitCut、Advice、Introduction、Weave、Target Object、Proxy Object等
  引介(Introduction)是指给一个现有类添加方法或字段属性,引介还可以在不改变现有类代码的情况下,让现有的Java类实现新的接口,或者为其指定一个父类从而实现多重继承。相对于增强(Advice)可以动态改变程序的功能或流程来说,引介(Introduction)则用来改变一个类的静态结构。比如我们可以让一个现有为实现java.lang.Cloneable接口,从而可以通过clone()方法复制这个类的实例。
拦截器是用来实现对连接点进行拦截,从而在连接点前或后加入自定义的切面模块功能。在大多数JAVA的AOP框架实现中,都是使用拦截器来实现字段访问及方法调用的拦截(interception)。所用作用于同一个连接点的多个拦截器组成一个连接器链(interceptor chain),链接上的每个拦截器通常会调用下一个拦截器。Spring AOP及JBoos AOP实现都是采用拦截器来实现的。
  面向对象编程(OOP)解决问题的重点在于对具体领域模型的抽象,而面向切面编程(AOP)解决问题的关键则在于对关注点的抽象。也就是说,系统中对于一些需要分散在多个不相关的模块中解决的共同问题,则交由AOP来解决;AOP能够使用一种更好的方式来解决OOP不能很好解决的横切关注点问题以及相关的设计难题来实现松散耦合。因此,面向方面编程 (AOP) 提供另外一种关于程序结构的思维完善了OOP,是OOP的一种扩展技术,弥补补了OOP的不足。
  AOP概念详解:注意以下实例aop:'开头的AspectJ的概念,Spring没有分的这么细.
  (1)方面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象。事务管理是一个很好的横切关注点例子。方面用Spring的Advisor或拦截器实现, 然后可以通过@Aspect标注或在applictionContext.xml中进行配置:

      <aop:aspect id="fourAdviceAspect" ref="fourAdviceBean" order="2"> 


  (2)连接点(Joinpoint):程序执行过程中的行为,如方法的调用或特定的异常被抛出,在代码上有JoinPoint类和ProceedingJoinPoint类,如下所示,可以通过JoinPoint获取很多参数,JoinPoint一般用在Advice实现方法中作为参数传入,ProceedingJoinPoint用于实现围绕Advice的参数传入。   通过下面JoinPoint的接口可以看出通过JoinPoint可以得到代理对象和Target对象。

package org.aspectj.lang;  
import org.aspectj.lang.reflect.SourceLocation;  
public interface JoinPoint {  
    String toString();         //连接点所在位置的相关信息  
    String toShortString();     //连接点所在位置的简短相关信息  
    String toLongString();     //连接点所在位置的全部相关信息  
    Object getThis();         //返回AOP代理对象  
    Object getTarget();       //返回目标对象  
    Object[] getArgs();       //返回被通知方法参数列表  
    Signature getSignature();  //返回当前连接点签名  
    SourceLocation getSourceLocation();//返回连接点方法所在类文件中的位置  
    String getKind();        //连接点类型  
    StaticPart getStaticPart(); //返回连接点静态部分  
}  
  
public interface ProceedingJoinPoint extends JoinPoint {  
    public Object proceed() throws Throwable;  
    public Object proceed(Object[] args) throws Throwable;  
}  


  (3)切入点(Pointcut):指定一个Adivce将被引发的一系列连接点的集合。AOP框架必须允许开发者指定切入点,例如,使用正则表达式。
1.xml中配置:

 <aop:pointcut id="myPointcut" expression="execution(*   com.wicresoft.app.service.impl.*.*(..))" method="release" /> 

2.使用Annoation :

    @pointcut("execution * transfer(..)")
并用一个返回值为void,方法体为空的方法来命名切入点如:  private void anyOldTransfer(){}
 之后就可以在Advice中引用,如: @AfterReturning(pointcut="anyOldTransfer()", returning="reVal")
package org.springframework.aop;  
public interface Pointcut {  
    ClassFilter getClassFilter();  
    MethodMatcher getMethodMatcher();  
    Pointcut TRUE = TruePointcut.INSTANCE;  
}  
package org.springframework.aop;  
public interface ClassFilter {  
     boolean matches(Class<?> clazz);//如果clazz与我们关注的现象相符时返回true,负责返回false  
     ClassFilter TRUE = TrueClassFilter.INSTANCE;//静态参数 如果类型对于要扑捉的Pointcut来说无所谓,可将此参数传递给Pointcut  
}  
package org.springframework.aop;  
public interface MethodMatcher {  
   boolean matches(Method method, Class<?> targetClass);  
  
 /** 
  * 是否对参数值敏感 
  * 如果为false表明匹配时不需要判断参数值(参数值不敏感),称之为StaticMethodMatcher,这时只有 
  * matches(Method method, Class<?> targetClass); 被执行,执行结果可以缓存已提高效率。 
  * 如果为true表明匹配时需要判断参数值(参数值敏感),称之为DynamicMethodMatcher,这时先执行 
  * matches(Method method, Class<?> targetClass);如果返回true,然后再执行 
  * boolean matches(Method method, Class<?> targetClass, Object[] args);已做进一步判断 
  *  
  */  
 boolean isRuntime();  
 boolean matches(Method method, Class<?> targetClass, Object[] args);  
 MethodMatcher TRUE = TrueMethodMatcher.INSTANCE;  
}  

(4)通知(Advice):在特定的连接点,AOP框架执行的动作。各种类型的通知包括“around”、“before”和“throws”通知。通知类型将在下面讨论。许多AOP框架包括Spring都是以拦截器做通知模型,维护一个“围绕”连接点的拦截器链。Advice中必须用到PointCut
在xml中配置,配置中的method为Aspect实现类中的方法名,使用pointcut自定义或pointcut-ref进行引用已有pointcut

 <aop:before pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="authority" /> 
       <aop:after pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="release" /> 
       <aop:after-returning pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="log" />
       <aop:around pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="processTx" /> 
       <aop:after-throwing pointcut-ref="myPointcut" method="doRecovertyActions" throwing="ex" />   

或使用Annoation:


       @AfterReturning(returning="rvt", pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))")
       @AfterThrowing(throwing="ex", pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))")
       @After("execution(* com.wicresoft.app.service.impl.*.*(..))")
       @Around("execution(* com.wicresoft.app.service.impl.*.*(..))")  

注意:
AfterReturning 增强处理处理只有在目标方法成功完成后才会被织入。
After 增强处理不管目标方法如何结束(保存成功完成和遇到异常中止两种情况),它都会被织入。

(5)引入(Introduction):添加方法或字段到被通知的类,引入新的接口到任何被通知的对象。例如,你可以使用一个引入使任何对象实现IsModified接口,来简化缓存。使用introduction要有三个步骤
  (1)声明新接口
  (2)创建自己的IntrouductionInterceptor通过Implements IntroductionInterceptor或extends DelegatingIntroductionInterceptor 并同时implements(1)中声明的接口
  (3)将新接口和自定义的IntroductionInterceptor配置到DefaultIntroductionAdvisor中,然后将前三者配置到ProxyFactoryBean中。

public interface IOtherBean {  
    public void doOther();  
}  
  
public class SomeBeanIntroductionInterceptor implements IOtherBean, IntroductionInterceptor {  
  
    public void doOther() {  
        System.out.println("doOther!");  
    }  
  
    public Object invoke(MethodInvocation invocation) throws Throwable {  
          
        //判断调用的方法是否为指定类中的方法  
        if ( implementsInterface(invocation.getMethod().getDeclaringClass()) ) {  
            return invocation.getMethod().invoke(this, invocation.getArguments());  
        }  
          
        return invocation.proceed();  
    }  
      
    /** 
     * 判断clazz是否为给定接口IOtherBean的实现 
     */  
    public boolean implementsInterface(Class clazz) {  
          
        return clazz.isAssignableFrom(IOtherBean.class);  
    }  
}  

xml 配置

<!-- 目标对象 -->  
<bean id="someBeanTarget" class="aop.spring.introduction.SomeBeanImpl" />  
<!-- 通知 -->  
<bean id="someBeanAdvice" class="aop.spring.introduction.SomeBeanIntroductionInterceptor" />  
<!-- 通知者,只能以构造器方法注入-->  
<bean id="introductionAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor">  
    <constructor-arg ref="someBeanAdvice" />  
    <constructor-arg value="aop.spring.introduction.IOtherBean" />      
</bean>  
<!-- 代理 (将我们的切面织入到目标对象)-->  
<bean id="someBeanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
    <!-- 若目标对象实现了代理接口,则可以提供代理接口的配置 -->  
    <property name="proxyInterfaces"  value="aop.spring.introduction.ISomeBean" />  
    <!-- 配置目标对象 -->  
    <property name="target" ref="someBeanTarget" />  
    <!-- 配置切面 -->  
    <property name="interceptorNames">  
        <list>  
            <value>introductionAdvisor</value>  
        </list>  
    </property>  
</bean>  


  (六)拦截器(Advisor )常用的有PointCutAdvisor和IntroudtionAdvisor。前者Advisor有PointCut和Advice组成,满足Poincut(指定了哪些方法需要增强),则执行相应的Advice(定义了增强的功能),后者由Introduction构成。PointCutAdvisor主要是根据PointCut中制定的Target Objects的方法在调用(前,后,around,throws, after-return等)时引入新的Aspect中的methods, 而IntroductionAdvisor主要是引入新的接口到Targets对象中。

上一篇下一篇

猜你喜欢

热点阅读