编程式AOP实例

LogOperation 日志注解

/**
 * <p>
 * <code>LogOperation</code>日志注解
 * </p>
 *
 * @author yhdiao
 * @version 1.0
 * @date 2020/10/22 14:18
 * @since 1.0
 */
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogOperation {
    
}

LogAdvisor 日志通知器

import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * <code>LogAdvisor</code> 日志通知器
 * </p>
 *
 * @author yhdiao
 * @version 1.0
 * @since 1.0
 */
public class LogAdvisor extends AbstractPointcutAdvisor {
    /**
     * 切点
     */
    private Pointcut pointcut;

    @Autowired
    LogAroundAdvice logAroundAdvice;

    public LogAdvisor() {
        this.pointcut = buildPointcut();
    }

    /**
     * <p>
     * <code>getPointcut</code> 切点
     * </p>
     *
     * @return {@link org.springframework.aop.Pointcut}
     * @author yhdiao
     */
    @Override
    public Pointcut getPointcut() {
        return this.pointcut;
    }

    /**
     * <p>
     * <code>getAdvice</code> 通知
     * </p>
     *
     * @return {@link org.aopalliance.aop.Advice}
     * @author yhdiao
     */
    @Override
    public Advice getAdvice() {
        //return new LogAroundAdvice(new LogOperationHandler());
        return this.logAroundAdvice;
    }

    /**
     * <p>
     * <code>buildPointcut</code> 创建切点
     * </p>
     *
     * @return {@link org.springframework.aop.Pointcut}
     * @author yhdiao
     */
    private Pointcut buildPointcut() {
        Pointcut cpc = new AnnotationMatchingPointcut(LogOperation.class, true);
        Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(LogOperation.class);
        return new ComposablePointcut(cpc).union(mpc);
    }
}

LogAroundAdvice 日志环绕通知

import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * <p>
 * <code>LogAroundAdvice</code> 日志环绕通知
 * </p>
 *
 * @author yhdiao
 * @version 1.0
 * @since 1.0
 */
@Slf4j
public class LogAroundAdvice implements MethodInterceptor {

    //@Autowired
    //private OperationHandler operationHandler;

    /*public LogAroundAdvice(OperationHandler operationHandler) {
        this.operationHandler = operationHandler;
    }*/

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        long beginTime = System.currentTimeMillis();
        Object result = invocation.proceed();
        long time = System.currentTimeMillis() - beginTime;
        // 日志业务处理
        //operationHandler.handle(invocation, time, result);
        return result;
    }
}

AopConfig 编程式aop配置

/**
 * <p>
 * <code>AopConfig</code> 编程式aop配置
 * </p>
 *
 * @author yhdiao
 * @version 1.0
 * @since 1.0
 */
@Configuration
public class AopConfig {

    @Bean
    public LogAdvisor advisor() {
        LogAdvisor advisor = new LogAdvisor();
        advisor.setOrder(0);
        return advisor;
    }
    @Bean
    public LogAroundAdvice logAroundAdvice() {
        return new LogAroundAdvice();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值