记一次使用AOP进行自定义注解遇到的问题-类注解失效

背景

在项目开发过程中,使用AOP自定义注解解决业务扩展问题,使用过程中,定义注解可以修饰类,发现修饰类的时候,切面不生效。

自定义注解

步骤一:导入依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

步骤二:创建注解

@Target({ElementType.TYPE, ElementType.METHOD})//用于指定被修饰的 Annotation 能用于修饰哪些程序元素
@Retention(RetentionPolicy.RUNTIME)//:指定所修饰的 Annotation 的生命周期, 只声明为RUNTIME生命周期的注解,才能通过反射获取。
@Documented
public @interface TestAnotation{
	//String[] value() default "";
}

步骤三:创建切入点
注解方式编写切入点:
将注解作为切入点,配置当前使用@annotation进行配置,但是如果需要注解修饰类,这样就会失效

@Pointcut("@annotation(com.liuliu.demo.aop.TestAnotation)")
public void myPointCut(){}

修饰类的切入点的编写需要替换成@within。

@annotation表明注解作用于方法上,如果注解作用于类上须换成@within,但注意@Pointcut注解中切入点的写法,可以用 “||” 进行连接。

@Pointcut("@annotation(com.liuliu.demo.aop.TestAnotation || @within(com.liuliu.demo.aop.TestAnotation)")

切入点还可以使用execution进行编写,具体编写规则自行查阅。

步骤四:创建通知
通知类型有@Around、@Before、@After、@AfterReturning、@AfterThrowing五种类型。

@Component
@Aspect
public class UserProxy{
    //前置通知
     //@Before("execution(* com.liuliu.demo.annocation_aop.User.add(..))")
     @Before("myPointCut()")
     public void before(){
        System.ouot.println("before....");
     }
    //返回通知,有异常不执行
    //@AfterReturning(value="execution(* com.liuliu.demo.annocation_aop.User.add(..))")
    @AfterReturning("myPointCut()")
    public void afterReturning(){
        System.out.println("afterreturning...");
    }
    //后置通知,有没有异常都会执行
    //@After(value="execution(* com.liuliu.demo.annocation_aop.User.add(..))")
    @After("myPointCut()")
    public void after(){
        System.out.println("after...");
    }
    //异常通知
    //@AfterTrowing(value="execution(* com.liuliu.demo.annocation_aop.User.add(..))")
    @AfterTrowing("myPointCut()")
    public void AfterTrowing(){
        System.out.println("AfterTrowing...");
    }
    //环绕通知,之前,之后都执行
    //@Around(value="execution(* com.liuliu.demo.annocation_aop.User.add(..))")
    @Around("myPointCut()")
    public JsonResultUtil around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕通知执行目标方法之前");
        /*获取注解中的参数值*/
        MethodSignature signature = (MethodSignature) point.getSignature();
        TestRequestAnnotation declaredAnnotation = signature.getMethod().getDeclaredAnnotation(TestRequestAnnotation.class);
        String value = declaredAnnotation.value();
        System.out.println("==注解@CustomAnno的value==" + value);

        /*获取通知的方法的参数*/
        Object[] args = point.getArgs();

        /*获取请求中的相关信息*/
        String ip = request.getRemoteAddr();
        String url = request.getRequestURL().toString();
        String method = request.getMethod();
        System.out.println("IP地址:" + ip);
        System.out.println("URL:" + url);
        System.out.println("METHOD:" + method);

        /*执行方法 并获取结果*/
        JsonResultUtil res = (JsonResultUtil) point.proceed();

        System.out.println("环绕通知执行目标方法之后");
        /*返回处理后的结果*/
        return res;
    }
}
  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值