spring实现aop的两种方式

1.在配置文件中配置

  • 增强类
package org.jgs1904.logger;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * @ClassName StudentServiceLogger
 * @Description TODO
 * @Author RenYuWen
 * @Since 2020/4/21 16:21
 */
public class StudentServiceLogger {

    private static Logger log = LogManager.getLogger();
    
    public void pointcut() {
    }

    /**
     * @Description: 前置增强方法
     * @param: [jp]
     * @return: void
     * @author: RenYuWen
     * @date: 2020/4/21 17:06
     */
    public void before(JoinPoint jp) {
        log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法被调用");
        log.info("传入的参数为:" + Arrays.toString(jp.getArgs()));
    }

    /**
     * @Description: 后置增强, 返回增强
     * @param: [jp, result]
     * @return: void
     * @author: RenYuWen
     * @date: 2020/4/21 17:09
     */
    public void aferReturning(JoinPoint jp, Object result) {
        log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行完毕");
        log.info("返回值是:" + result);
    }

    /**
     * @Description: 异常抛出增强
     * @param: [jp, e]
     * @return: void
     * @author: RenYuWen
     * @date: 2020/4/21 17:11
     */
    public void aferThrowing(JoinPoint jp, RuntimeException e) {
        log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行异常");
        log.info("异常是: "  + e);
    }

    /**
     * @Description: 最终增强
     * @param: [jp]
     * @return: void
     * @author: RenYuWen
     * @date: 2020/4/21 17:12
     */
    public void afer(JoinPoint jp) {
        log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行结束");
    }

    /**
     * @Description: 环绕增强
     * @param: [jp]
     * @return: java.lang.Object
     * @author: RenYuWen
     * @date: 2020/4/21 17:30
     */
    public Object around(ProceedingJoinPoint jp) throws Throwable {
        log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法被调用");
        log.info("传入的参数为:" + Arrays.toString(jp.getArgs()));

        try {
            Object result = jp.proceed();
            log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行完毕");
            log.info("返回值是:" + result);
            return result;
        } catch (Throwable e) {
            log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行异常");
            log.info("异常是: "  + e);
            throw e;
        } finally {
            log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行结束");
        }
    }
 }

  • 配置


 <bean id="studentServiceLogger" class="org.jgs1904.logger.StudentServiceLogger">bean>

 
 <aop:config>
     
     
     <aop:pointcut id="pointcut1" expression="execution(* org.jgs1904.service..*.*(..))"/>
     
     <aop:aspect ref="studentServiceLogger">
         
         <aop:before method="before" pointcut-ref="pointcut1">aop:before>
         
         <aop:after-returning method="aferReturning" pointcut-ref="pointcut1" returning="result">aop:after-returning>
         
         <aop:after-throwing method="aferThrowing" pointcut-ref="pointcut1" throwing="e">aop:after-throwing>
         
         <aop:after method="afer" pointcut-ref="pointcut1">aop:after>
         
         
     aop:aspect>
 aop:config>

2.注解配置

  • 增强类
package org.jgs1904.logger;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * @ClassName StudentServiceLogger
 * @Description TODO
 * @Author RenYuWen
 * @Since 2020/4/21 16:21
 */
@Aspect
public class StudentServiceLogger {

    private static Logger log = LogManager.getLogger();

    @Pointcut("execution(* org.jgs1904.service..*.*(..))")
    public void pointcut() {
    }

    /**
     * @Description: 前置增强方法
     * @param: [jp]
     * @return: void
     * @author: RenYuWen
     * @date: 2020/4/21 17:06
     */
    @Before("pointcut()")
    public void before(JoinPoint jp) {
        log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法被调用");
        log.info("传入的参数为:" + Arrays.toString(jp.getArgs()));
    }

    /**
     * @Description: 后置增强, 返回增强
     * @param: [jp, result]
     * @return: void
     * @author: RenYuWen
     * @date: 2020/4/21 17:09
     */
    @AfterReturning(value = "pointcut()", returning = "result")
    public void aferReturning(JoinPoint jp, Object result) {
        log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行完毕");
        log.info("返回值是:" + result);
    }

    /**
     * @Description: 异常抛出增强
     * @param: [jp, e]
     * @return: void
     * @author: RenYuWen
     * @date: 2020/4/21 17:11
     */
    @AfterThrowing(value = "pointcut()", throwing = "e")
    public void aferThrowing(JoinPoint jp, RuntimeException e) {
        log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行异常");
        log.info("异常是: "  + e);
    }

    /**
     * @Description: 最终增强
     * @param: [jp]
     * @return: void
     * @author: RenYuWen
     * @date: 2020/4/21 17:12
     */
    @After("pointcut()")
    public void afer(JoinPoint jp) {
        log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行结束");
    }

    /**
     * @Description: 环绕增强
     * @param: [jp]
     * @return: java.lang.Object
     * @author: RenYuWen
     * @date: 2020/4/21 17:30
     */
    // @Around("pointcut()") // 上面已经分别配置四种增强了,不要重复配置
    public Object around(ProceedingJoinPoint jp) throws Throwable {
        log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法被调用");
        log.info("传入的参数为:" + Arrays.toString(jp.getArgs()));

        try {
            Object result = jp.proceed();
            log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行完毕");
            log.info("返回值是:" + result);
            return result;
        } catch (Throwable e) {
            log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行异常");
            log.info("异常是: "  + e);
            throw e;
        } finally {
            log.info(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行结束");
        }
    }
 }

  • 配置

<bean id="studentServiceLogger" class="org.jgs1904.logger.StudentServiceLogger">bean>

<aop:aspectj-autoproxy>aop:aspectj-autoproxy>

你可能感兴趣的:(spring,aop)