@Around 切入点未调用自定义注释 [英] @Around Pointcut not getting invoked for custom annotation

查看:24
本文介绍了@Around 切入点未调用自定义注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题似乎是重复的,但现有问题的答案均​​无效.我正在创建一个自定义注释,如下所示,

The question seems to be duplicate but none of the answers from existing questions worked. I am creating a custom annotation as below,

@Target({ ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Translate {
}

我创建了一个方面,

@Aspect
@Configuration
public class TranslateAspect {

    @Around("@annotation(translate)")
    public Object translate(ProceedingJoinPoint joinPoint, Translate translate) throws Throwable {
        Object result = joinPoint.proceed();
        System.out.println(result); //Other logic
        return result;
    }
}

我也尝试为包提供完整的类名.实体被传递给 RestController,

I tried providing the complete class name with the package also. The entity is getting passed to RestController,

@Entity
@Getter
@Setter
public class Pojo {
    @Translate
    private String label;
 }

但是每当服务新请求时,都不会调用 translate 方法.

But the translate method is not getting invoked whenever the new request is served.

非常感谢您对此的任何帮助.

Highly appreciate any help around this.

推荐答案

来自参考文档:5.2.Spring AOP 能力和目标

Spring AOP 目前仅支持方法执行连接点(建议在 Spring bean 上执行方法).未实现字段拦截,尽管可以在不破坏核心 Spring AOP API 的情况下添加对字段拦截的支持.如果您需要建议字段访问和更新连接点,请考虑使用 AspectJ 之类的语言.

Spring AOP 与 Spring 容器管理的 bean 和建议方法执行一起使用.这里共享的代码注释了一个字段,而不是相应的 setter 方法.定义的 PCD 用于执行任何用 @Translate 注释的 Spring 容器管理的 bean 方法.

Spring AOP works with spring container managed beans and advicing method executions. The code shared here annotates a field and not the corresponding setter method. PCD defined is for the execution any Spring container managed bean method annotated with @Translate.

使用 @Entity 注释的类不会将其实例注册为 Spring 托管 bean.请仔细阅读此StackOverflow Q&A

A class annotated with @Entity will not register its instance as a Spring managed bean. Do go through this StackOverflow Q&A

Pojo 实例不是 Spring 管理的 bean(João Dias 在他的回答中也指出).

Pojo instance is not a Spring managed bean ( also pointed out by João Dias in his answer ).

这篇关于@Around 切入点未调用自定义注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆