SpringAOP“within”不能与方法一起工作

s1ag04yj  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(301)

下面是我的自定义注解。

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Transactional(value = TransactionalCode.MANAGER, readOnly = true)
public @interface FinanceReadTx {
}

我想对“myannotation”做些什么,所以我声明了@around和如下所示的方法。

@Aspect
@Component
public class TransactionalInterceptor implements Ordered {
    @Around("within(@org.springframework.transaction.annotation.Transactional *) || " +
            "within(@(@org.springframework.transaction.annotation.Transactional *) *)")
    public Object proceed(ProceedingJoinPoint pjp) throws Throwable {
        try {
            setDbType(pjp);
            Object result = pjp.proceed();
            DataSourceContextHolder.clearDataSourceType();
            return result;
        } finally {
            // restore state
            DataSourceContextHolder.clearDataSourceType();
        }
    }
....
}

下面的服务由其他类“自动连接”。所以我认为这不是aop代理的问题。

@Service
public class UnconfirmedReportService {
    private static final int PREVIEW_SIZE = 8;
    @Autowired
    private UnconfirmedReportRepository unconfirmedReportRepository;
...
    @FinanceHikariReadTx
    public List<UnconfirmedExcelDownloadView> getExcelData(UnconfirmedSearchCondition condition) {
        List<UnconfirmedExcelDownloadView> excelData = newArrayList();
        excelData.addAll(newArrayList(getPurchaseReportDetailExcel(condition)));
        return excelData;
    }
...
}

下面的代码调用上述服务

@Slf4j
@Component
public class UnconfirmedDashboardDetailExcelReader extends SellerExcelReaderTemplate<UnconfirmedExcelDownloadView, UnconfirmedSearchCondition> {
    @Autowired
    private UnconfirmedReportService unconfirmedReportservice;

    @Override public List<UnconfirmedExcelDownloadView> read(String conditionJson) {
        UnconfirmedSearchCondition condition = transformCondition(conditionJson);
        List<UnconfirmedExcelDownloadView> viewList = unconfirmedReportservice.getExcelData(condition);
        return viewList;
    }
...
}

如果@myannotation被注解到一个类中,则调用procedue(),但是如果一个方法带有类似于上面代码的注解,则它不起作用。我希望它只使用方法。
我几个小时来一直在努力解决这个问题,但找不到解决办法。请帮帮我。

um6iljoc

um6iljoc1#

您目前正在做一些类似于我在这个答案中解释的事情,即在类上匹配(meta)注解。
现在您想知道为什么它与方法不匹配。我在这里解释过。基本上, @within() 匹配带注解类中的任何内容 @annotation() 匹配带注解的方法。问题是, @annotation() 需要确切的类型名。
但是有另一种方法可以直接在 execution() 签名。在这里,您还可以选择指定元注解,方法与在注解类中使用元注解类似。让我们比较一下这两者:

package de.scrum_master.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class MetaAnnotationInterceptor {
  @Before(
    "execution(* *(..)) && (" +
      "within(@de.scrum_master.app.MetaAnnotation *) || " +
      "within(@(@de.scrum_master.app.MetaAnnotation *) *) || " +
      "within(@(@(@de.scrum_master.app.MetaAnnotation *) *) *)" +
    ")"
  )
  public void annotatedClasses(JoinPoint thisJoinPoint){
    System.out.println(thisJoinPoint);
  }

  @Before(
    "execution(@de.scrum_master.app.MetaAnnotation * *(..)) || " +
    "execution(@(@de.scrum_master.app.MetaAnnotation *) * *(..)) || " +
    "execution(@(@(@de.scrum_master.app.MetaAnnotation *) *) * *(..)) "
  )
  public void annotatedMethods(JoinPoint thisJoinPoint){
    System.out.println(thisJoinPoint);
  }
}

你要找的是后者。只要替换一下 de.scrum_master.app.MetaAnnotationorg.springframework.transaction.annotation.Transactional ,它应该适用于您的用例。一定不要弄乱了数据的编号和嵌套顺序 () , @ 以及 * ,否则很快就会出现切入点语法错误。
如果希望使用一个或两个advice方法,可以创建一个包含两个切入点的大凌乱字符串,也可以定义两个单独的方法 @Pointcut 并将它们组合到建议中,用 || .

相关问题