spring aop原理及使用

来源:互联网 发布:璟璞网络瑾萱 编辑:程序博客网 时间:2024/06/06 15:43

一、AOP概念

AOP(Aspect Orient Programming),也就是面向切面编程。

二、AOP术语

通知(Advice):通知定义了切面是什么以及何时使用。除了描述切面要完成的工作,通知还解决了何时执行这个工作的问题。它应该在某个方法被调用之前?之后?之前和之后?或是在方法抛出异常时。

连接点(Joinpoint):连接点是在程序执行过程中能够插入切面的一个点。这个点可以是方法被调用时,异常被抛出时、甚至字段被编辑时。切面代码可以通过这些点插入到程序的一般流程中,从而添加新的行为。

切入点(Pointcut):如果通知定义了切面的“什么”和“何时”,那么切入点就定义了“何地”。切入点的定义匹配通知要织入一个或多个连接点。我们通常使用明确的类和方法名称,或利用正则表达式定义匹配的类和方法名称模板来指定这些切入点。

切面(Aspect):切面是切入点和通知的结合。通知和切入点共同定义了关于切面的全部内容——它的功能、在何时和何地完成其功能。

引入(Introduction):“引入”允许我们向现有的类添加新的方法或属性。

目标(target):被通知的对象。

代理(proxy):向目标对象应用通知之后被创建的对象。

织入(Weaving):织入是把切面应用到目标对象来创建新的代理对象的过程。

三、AOP原理


<img src="AOP代理.jpg" />

如图,AOP 实际上是由目标类的代理类实现的AOP 代理其实是由 AOP 框架动态生成的一个对象,该对象可作为目标对象使用。AOP 代理包含了目标对象的全部方法,但 AOP 代理中的方法与目标对象的方法存在差异,AOP 方法在特定切入点添加了增强处理,并回调了目标对象的方法

四、Spring对AOP的支持:

Spring对AOP的支持局限于方法注入。如果我们的AOP需求超过简单方法注入的范畴(比如构造器或属性注入),就应该考虑在AspectJ里实现切面,利用Spring的从属注入把Spring的Bean注入到Aspect切面。
Spring生成被代理的方式有两种。如果目标对象实现的是一个接口,Spring会使用JDK的java.lang.reflect.Proxy类,它允许Spring动态生成一个新类来实现必要的接口、织入任何通知、并把对这些接口的任何调用都转发到目标类。
如果目标类不是实现一个接口,Spring就使用CGLB库生成目标类的一个子类。在创建这个子类时Spring织入通知,并把对这个子类的调用委托到目标类。
AOP编程其实很简单的事情。纵观AOP编程,其中需要程序员参与的只有三个部分:
定义普通组件义务;定义切入点,一个切入点可能横切多个业务组件;定义增强处理,增强处理就是在AOP框架为普通业务组织织入的处理动作。
所以进行AOP编程的关键就是定义切入点和定义增强处理。一旦定义了合适的切入点和增强处理,AOP框架将会自动生成AOP代理,即:代理对象方法=增强处理+被代理对象的方法。

五、Spring中AOP的实现

Spring 有如下两种选择来定义切入点和增强处理。

基于 Annotation 的“零配置”方式:使用@Aspect、@Pointcut等 Annotation 来标注切入点和增强处理。
基于 XML 配置文件的管理方式:使用 Spring 配置文件来定义切入点和增强点。

Spring的AOP里有5个类型的通知,用于指定相对于连接点何时运行通知。

Before(前),After-returning(返回后),After-throwing(抛出后),Aroud(周围),Introduction(引入)

①基于Annotation的“零配置”方式。

首先启用 Spring 对 @AspectJ 切面配置的支持。

<aop:aspectj-autoproxy/> 

或者引入<bean class="org.springframeword.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
②自动代理@AspectJ切面
@Aspect
public class Audience {
public Audience() {}

@Pointcut("execution(* *.perform(..))")
public void performance() {}

@Before("performance()")
public void takeSeats(){
System.out.println("The audience is taking their seats.");
}

@Before("performance()")
public void turnOffCellPhones(){
System.out.println("The audience is turning  off their cellphones.");
}

@AfterReturning("performance()")
public void applaud() {
System.out.println("CLAP CLAP CLAP CLAP CLAP CLAP");
}

@AfterThrowing("performance()")
public void demandRefund() {
System.out.println("Boo! We want our money back")
}
}

使用@Around注解时如下:
@Around("performance()")
public void watchPerformance(ProceedingJoinPoint joinpoint){
System.out.println("The audience is taking their seats.");
System.out.println("The audience is turning  off their cellphones.");
try{
joinpoint.proceed();
}catch(PerformanceException throwable){
System.out.println("CLAP CLAP CLAP CLAP CLAP CLAP");
}
}
要充当周围通知的方法需要必须接收一个ProceedingJoinPoint对象作为参数,然后调用这个对象的proceed()方法。
@Pointcut注解在@Aspect切面里定义一个可重用的切点。赋给@Pointcut注解的值是一个AspectJ切点表达式,表示这个切点应该匹配任意一个类的perform()方法。这个切点的名称是源自注解所应用的方法名称,所以本例中切点名称就是performance()。实际上performance()方法与此并不相关。

切入点表达式”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包下,返回值为任意类型;方法名任意;参数不作限制的所有方法。


②基于 XML 配置文件的管理方式


不配置切入点
<?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-3.0.xsd  
           http://www.springframework.org/schema/aop  
       http://www.springframework.org/schema/beans/spring-aop-3.0.xsd">  
        <aop:config>  
            <!-- 将 fourAdviceBean 转换成切面 Bean, 切面 Bean 的新名称为:fourAdviceAspect,指定该切面的优先级为2 -->  
            <aop:aspect id="fourAdviceAspect" ref="fourAdviceBean" order="2">  
                <!-- 定义个After增强处理,直接指定切入点表达式,以切面 Bean 中的 Release() 方法作为增强处理方法 -->  
                <aop:after pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="release" />  
                  
                <!-- 定义个Before增强处理,直接指定切入点表达式,以切面 Bean 中的 authority() 方法作为增强处理方法 -->  
                <aop:before pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="authority" />  
                  
                <!-- 定义个AfterReturning增强处理,直接指定切入点表达式,以切面 Bean 中的 log() 方法作为增强处理方法 -->  
                <aop:after-returning pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="log" />  
                  
                <!-- 定义个Around增强处理,直接指定切入点表达式,以切面 Bean 中的 processTx() 方法作为增强处理方法 -->  
                <aop:around pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="processTx" />  
                  
            </aop:aspect>  
        </aop:config>  
          
        <!-- 省略各个Bean 的配置 -->  
        <!-- ... -->       
</beans>  
配置切入点
<aop:config>  
<!-- 定义一个切入点,myPointcut,直接知道它对应的切入点表达式 -->  
<aop:pointcut id="myPointcut" expression="execution(* com.wicresoft.app.service.impl.*.*(..))" method="release" />  
<aop:aspect id="afterThrowingAdviceAspect" ref="afterThrowingAdviceBean" order="1">  
<!-- 使用上面定于切入点定义增强处理 -->  
<!-- 定义一个AfterThrowing 增强处理,指定切入点以切面 Bean 中的 doRecovertyActions() 方法作为增强处理方法 -->  
<aop:after-throwing pointcut-ref="myPointcut" method="doRecovertyActions" throwing="ex" />  
</aop:aspect>  
</aop:config>  






0 0
原创粉丝点击