下面是我的自定义注释。
@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();
}
}
// ...
}
以下服务由其他类“ Autowiring ”。所以我认为这不是与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
被注释到一个类,则调用proceed(),但如果一个方法带有像上面代码这样的注释,则它不起作用。我希望它仅适用于方法。
我尝试如何解决这个问题?
最佳答案
您当前正在执行类似于我在 this answer 中解释的操作,即匹配类上的(元)注释。
现在您想知道为什么它与方法不匹配。我解释说here 。基本上,@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.MetaAnnotation 替换为 org.springframework.transaction.annotation.Transactional 即可,它应该适合您的用例。确保不要弄乱 ()
、@
和 *
的数量和嵌套顺序,否则很快就会出现切入点语法错误。
如果您更喜欢使用一种或两种建议方法,则可以创建一个包含两个切入点的大而困惑的字符串,或者定义两个单独的 @Pointcut
并将它们组合在建议中,将它们链接起来与||
。
关于java - 如何拦截类型或方法的元注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66996130/