spring aop实现

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

基于 Annotation 的“零配置”方式:使用@Aspect、@Pointcut等 Annotation 来标注切入点和增强处理。
基于 XML 配置文件的管理方式:使用 Spring 配置文件来定义切入点和增强点。
1、基于 Annotation 的“零配置”方式。
(1)、首先启用 Spring 对 @AspectJ 切面配置的支持。



aop:aspectj-autoproxy/

如果不打算使用 Spring 的 XML Schema 配置方式,则应该在 Spring 配置文件中增加如下片段来启用@AspectJ 支持。

(2)、定义切面 Bean。
当启动了@AspectJ 支持后,只要在 Spring 容器中配置一个带@Aspect 注释的 Bean, Spring 将会自动识别该 Bean 并作为切面处理。

// 使用@Aspect 定义一个切面类
@Aspect
public class LogAspect {
// 定义该类的其他内容
...
}

(3)、定义 Before 增强处理。

// 定义一个切面
@Aspect
@Component
public class BeforeAdviceTest {
// 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点
@Before("execution(* com.wicresoft.app.service.impl..(..))")
public void authorith(){
System.out.println("模拟进行权限检查。");
}
}

上面使用@Before Annotation 时,直接指定了切入点表达式,指定匹配 com.wicresoft.app.service.impl包下所有类的所有方法执行作为切入点。
关于这个表达式的规则如下图。

(4)、定义 AfterReturning 增强处理。

// 定义一个切面
@Aspect
@Component
public class AfterReturningAdviceTest {
// 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点
@AfterReturning(returning="rvt", pointcut="execution(* com.wicresoft.app.service.impl..(..))")
public void log(Object rvt) {
System.out.println("模拟目标方法返回值:" + rvt);
System.out.println("模拟记录日志功能...");
}
}

(5)、定义 AfterThrowing 增强处理。

// 定义一个切面
@Aspect
@Component
public class AfterThrowingAdviceTest {
// 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点
@AfterThrowing(throwing="ex", pointcut="execution(* com.wicresoft.app.service.impl..(..))")
public void doRecoverActions(Throwable ex) {
System.out.println("目标方法中抛出的异常:" + ex);
System.out.println("模拟抛出异常后的增强处理...");
}
}

(6)、定义 After 增强处理。
After 增强处理与AfterReturning 增强处理有点相似,但也有区别:
AfterReturning 增强处理处理只有在目标方法成功完成后才会被织入。
After 增强处理不管目标方法如何结束(保存成功完成和遇到异常中止两种情况),它都会被织入。

// 定义一个切面
@Aspect
@Component
public class AfterAdviceTest {
// 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点
@After("execution(* com.wicresoft.app.service.impl..(..))")
public void release() {
System.out.println("模拟方法结束后的释放资源...");
}
}

(7)、Around 增强处理
Around 增强处理近似等于 Before 增强处理和 AfterReturning 增强处理的总和。它可改变执行目标方法的参数值,也可改变目标方法之后的返回值。

// 定义一个切面
@Aspect
@Component
public class AroundAdviceTest {
// 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点
@Around("execution(* com.wicresoft.app.service.impl..(..))")
public Object processTx(ProceedingJoinPoint jp) throws java.lang.Throwable {
System.out.println("执行目标方法之前,模拟开始事物...");
// 执行目标方法,并保存目标方法执行后的返回值
Object rvt = jp.proceed(new String[]{"被改变的参数"});
System.out.println("执行目标方法之前,模拟结束事物...");
return rvt + "新增的内容";
}
}

(8)、访问目标方法的参数。
访问目标方法最简单的做法是定义增强处理方法时将第一个参数定义为 JoinPoint 类型,当该增强处理方法被调用时,该 JoinPoint 参数就代表了织入增强处理的连接点。JoinPoint 里包含了如下几个常用方法。

Object[] getArgs(): 返回执行目标方法时的参数。
Signature getSignature(): 返回被增强的方法的相关信息。
Object getTarget(): 返回被织入增强处理的目标对象。
Object getThis(): 返回 AOP 框架为目标对象生成的代理对象。
提示:当时使用 Around 处理时,我们需要将第一个参数定义为 ProceedingJoinPoint 类型,该类型是 JoinPoint 类型的子类。

(9)、定义切入点。
所谓切入点,其实质就是为一个切入点表达式起一个名称,从而允许在多个增强处理中重用该名称。

Spring 切入点定义包含两个部分:

一个切入点表达式。
一个包含名字和任意参数的方法签名。

// 使用@Pointcut Annotation 时指定切入点表达式
@pointcut("execution * transfer(..)")
// 使用一个返回值为void,方法体为空的方法来命名切入点
private void anyOldTransfer(){}

// 使用上面定义的切入点
@AfterReturning(pointcut="anyOldTransfer()", returning="reVal")
public void writeLog(String msg, Object reVal){
...
}

2、基于 XML 配置文件的管理方式。
不配置切入点


aop:config

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

<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 的配置 -->  
    <!-- ... -->  

配置切入点


aop:config

<aop:pointcut id="myPointcut" expression="execution(* com.wicresoft.app.service.impl..(..))" method="release" />
<aop:aspect id="afterThrowingAdviceAspect" ref="afterThrowingAdviceBean" order="1">


<aop:after-throwing pointcut-ref="myPointcut" method="doRecovertyActions" throwing="ex" />
</aop:aspect>
</aop:config>

    <!-- 省略各个Bean 的配置 -->  
    <!-- ... -->

此文章摘自http://blog.csdn.net/a906998248/article/details/7514969

posted @ 2017-01-03 18:07  SheaChen  阅读(128)  评论(0编辑  收藏  举报