方面不会在我的应用程序中的存储库周围触发

问题描述 投票:0回答:1

我想触发用存储库注释并属于我的包的类的方面,例如这个:

//com.foo.myapp.bar.repositories.dao
@Repository
public class MyRepo extends JpaRepository<MyEntity, String>{

我的班级是这样创建的jpa存储库:

@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = "firstManagerFactory",
    transactionManagerRef = "firstTransactionManager",
    basePackages = {"com.foo.myapp.bar.repositories.first.dao"}

)公共类DbConfig {

我的方面如下,但仅当我离开repository()切入点时才激活,但是如果我还指定应用程序包,则它不起作用:

@Pointcut("within(@org.springframework.stereotype.Repository *)")
private void repositoryInvocation() {
    // Method is empty as this is just a Pointcut, the implementations are in the advices.
}

@Pointcut("within(com.foo.myapp..*)")
public void applicationPackage() {
    // Method is empty as this is just a Pointcut, the implementations are in the advices.
}

@Around("repositoryInvocation() && applicationPackage()") //this && doesn't work, I have to remove the second one
public Object aspectTriggers(ProceedingJoinPoint joinPoint) throws Throwable {
    Object result = joinPoint.proceed();
    return result;
}

我想念什么?

编辑:

我想我明白了:问题是存储库的实现不属于我的应用程序包,而是属于Spring的SimpleJPARepository。就像方面仅在实现上起作用,而完全忽略了接口。

aspectj spring-aop
1个回答
0
投票

我想你不想要

@Pointcut("within(@org.springframework.stereotype.Repository *)")

而是

@Pointcut("@within(org.springframework.stereotype.Repository)")

注意切入点语法,两者是不相同

  • [within()描述您要限制/限制切入点的包或类名。
  • [@within()寻找具有给定注释的类型(类)。

您想要后者,而不是前者。


Edit:再考虑一下,实际上我看不出第一个版本不起作用的明显原因,即使它比第二个版本复杂一些。]

但是您说过,第二切入点还是有问题。您是否100%确定您的存储库类确实在com.foo.myapp(子)包中?包名称或切入点中没有错字?实际上,如果不尝试并且仅查看它,它应该可以否则起作用。

© www.soinside.com 2019 - 2024. All rights reserved.