【问题标题】:Pointcut on @Query annotation of JPA repositoryJPA 存储库的 @Query 注释上的切入点
【发布时间】:2022-12-04 22:28:54
【问题描述】:

我正在尝试为 JPA 存储库方法添加自定义注释以获得关于 @Query 值的建议。

下面是我试过的一段代码

MyFilterAspect 类

@Aspect
@Component
public class MyFilterAspect {
   @Pointcut("execution(* *(..)) && @within(org.springframework.data.jpa.repository.Query)")
   private void createQuery(){}

   @Around("createQuery()")
   public void applyFilter(JointPoint jp) {
   }
}

存储库代码

@MyFilter
@Query(Select * ...)
MyObject findByNameAndClass(...)

所以我不断收到错误

createQuery() is never called At MyFilterAspect

我正在尝试使用建议更新查询值。

我究竟做错了什么?

【问题讨论】:

    标签: sql jpa spring-data-jpa spring-aop aspect


    【解决方案1】:

    您不应为此目的使用 @within 注释。相反,您应该使用 @annotation,如下所示:

    @Pointcut("execution(* *(..)) && @annotation(org.springframework.data.jpa.repository.Query)")
    private void createQuery(){}
    

    此外,您应该使用 JoinPoint 访问方法签名,然后您可以从签名中提取注释。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 2017-05-09
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多