Spring_环绕通知

Posted 大梦几千秋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring_环绕通知相关的知识,希望对你有一定的参考价值。

业精于勤疏于嬉,行成于思毁于随。

创建环绕通知

  环绕通知是最强大的通知类型,它能够让你所编写的逻辑将被通知的目标方法完全包装起来,实际它就像在一个通知方法中同时编写前置通知和后置通知。

  Spring使用@Around注解声明环绕通知。

package chapter4.practice1;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
 * POJO
 * 使用@Aspect标注该POJO也是切面
 */
@Aspect
public class Audience {
    /**
     * 使用@Pointcut设置切点表达式
     */
    @Pointcut("execution(** chapter4.practice1.Performance.perform(..))")
    public void performance() {}
    
    @Around("performance()")
    public void watchPerformance(ProceedingJoinPoint jp) {
        try {
            System.out.println("Siliencing cell phones...");
            System.out.println("Taking seats...");
        //目标方法执行前 jp.proceed();
        //目标方法执行后 System.out.println(
"CLAP CLAP..."); } catch (Throwable e) { System.out.println("Demanding a refund..."); } } }

 

ProceedingJoinPoint的proceed()方法

/*******************************************************************************
 * Copyright (c) 2005 Contributors.
 * All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution and is available at
 * http://eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * initial implementation              Alexandre Vasseur
 *******************************************************************************/
package org.aspectj.lang;

import org.aspectj.runtime.internal.AroundClosure;

/**
 * ProceedingJoinPoint exposes the proceed(..) method in order to support around advice in @AJ aspects
 *
 * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
 */
public interface ProceedingJoinPoint extends JoinPoint {

    /**
     * The joinpoint needs to know about its closure so that proceed can delegate to closure.run()
     * <p/>
     * This internal method should not be called directly, and won‘t be visible to the end-user when
     * packed in a jar (synthetic method)
     *
     * @param arc
     */
    void set$AroundClosure(AroundClosure arc);

    /**
     * Proceed with the next advice or target method invocation
     *
     * @return
     * @throws Throwable
     */
    public Object proceed() throws Throwable;

    /**
     * Proceed with the next advice or target method invocation
     * <p/>
     * <p>Unlike code style, proceed(..) in annotation style places different requirements on the 
     * parameters passed to it.  The proceed(..) call takes, in this order:
     * <ul>
     * <li> If ‘this()‘ was used in the pointcut for binding, it must be passed first in proceed(..).
     * <li> If ‘target()‘ was used in the pointcut for binding, it must be passed next in proceed(..) - 
     * it will be the first argument to proceed(..) if this() was not used for binding.
     * <li> Finally come all the arguments expected at the join point, in the order they are supplied 
     * at the join point. Effectively the advice signature is ignored - it doesn‘t matter 
     * if a subset of arguments were bound or the ordering was changed in the advice signature, 
     * the proceed(..) calls takes all of them in the right order for the join point. 
     * </ul>
     * <p>Since proceed(..) in this case takes an Object array, AspectJ cannot do as much 
     * compile time checking as it can for code style. If the rules above aren‘t obeyed 
     * then it will unfortunately manifest as a runtime error. 
     * </p>
     *
     * @param args
     * @return
     * @throws Throwable
     */
    public Object proceed(Object[] args) throws Throwable;

}

  proceed()方法会阻塞对被通知方法的调用。

  



以上是关于Spring_环绕通知的主要内容,如果未能解决你的问题,请参考以下文章

Spring(十七):Spring AOP:通知(前置后置返回异常环绕)

Spring(十七):Spring AOP:通知(前置后置返回异常环绕)

spring: 创建环绕通知

Spring的环绕通知

[原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知

spring框架应用系列四:切面编程(环绕通知与前后置通知区别)