手写Spring注解实现切面动态拦截

手写Spring注解实现Spring AOP切面动态拦截

1.新建注解类命名为PointCutAddress
package com.map.common.annotation;
import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PointCutAddress {

}

这里要注意@Target,@Retention,@Documented在新加注解功能上的实际用途,不懂的可以百度其他博客,这里不做过多赘述。

2.创建切面Aspect类
package com.map.dotwith.map.aspect;

@Aspect
@Component
public class requestAspect {

    // 配置织入点  添加刚才创建的注解的路径
    @Pointcut("@annotation(com.map.common.annotation.PointCutAddress)")
    public void logPointCut()
    {
    }

    /**
     * 处理完请求后执行
     * 这里使用around
     * 不同的功能使用不同的位置切入吧
     * 但是要注意的是 @Before、@After、@Around三者传入的参数类型是不一样的
     * @param joinPoint 切点
     */
    @Around("logPointCut()")
    public Object doAfterReturning(ProceedingJoinPoint joinPoint)
    {
        Object result = "";
        try {
        	// 获取执行方法的返回数据
            result = joinPoint.proceed();
           	// 这里做你的逻辑判断 修改执行方法得返回结果
            if (**********){
                result = "********";
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return result;
    }

}

3.注解添加

这里很简单了,跟普通注解一样的,想在哪里切入就把注解添加在哪个方法上就行,调用这个方法就会进去切面执行切面内的逻辑。

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值