不同 Maven 模块中的方法的 getAnnotation returns null
getAnnotation returns null for methods in different maven module
我在 Maven 模块中创建了以下注释 "A"
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface CacheDelete {...}
在同一模块 "A" 中,我有 TestPojo class,我正在其中使用此注释
@CacheDelete()
public void removeTestPojo(int id) {
}
我正在从模块 "A" 的测试用例中调用此 removeTestPojo() 方法。这里一切正常。我在建议中使用以下代码获得了正确的注释。
建议方法代码 CacheAspect class 模块 "A":
CacheDelete cacheDeleteAnnotation = getAnnotation((MethodSignature) joinPoint.getSignature(),
CacheDelete.class);
获取注解方法:
private <T extends Annotation> T getAnnotation(MethodSignature methodSignature,
Class<T> annotationClass) {
return methodSignature.getMethod().getAnnotation(annotationClass);
}
问题:
现在我有一个不同的模块 "B",我在其中使用 "A" 的依赖项并且 "B" 模块中的方法之一用 @CacheDelete 注释。
当我 运行 模块 "B" 中的测试用例用于注释方法并调试 CacheAspect class 时,调试点出现在我的建议中,但我得到注释 returns 此处为空。
谁知道可能是什么原因?
得到问题,与不同模块无关。我注释的方法是接口的实现,并通过接口引用变量调用实现的方法。
所以当你使用:
(MethodSignature) proceedingJoinPoint.getSignature().getMethod()
它returns来自接口的方法;
相反,我将上面的代码替换为:
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = signature.getMethod();
String methodName = method.getName();
if (method.getDeclaringClass().isInterface()) {
method = proceedingJoinPoint.getTarget().getClass().getDeclaredMethod(methodName,
method.getParameterTypes());
}
因此,这将检查方法是否为接口,如果是,我将调用:
proceedingJoinPoint.getTarget().getClass().getDeclaredMethod()
这给了我子类的方法。
奇怪的是,当我们通过接口调用子类方法时,当子类方法中使用注解时调用传播到通知(AOP),但注解不会在子类方法中传播。
我在 Maven 模块中创建了以下注释 "A"
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface CacheDelete {...}
在同一模块 "A" 中,我有 TestPojo class,我正在其中使用此注释
@CacheDelete()
public void removeTestPojo(int id) {
}
我正在从模块 "A" 的测试用例中调用此 removeTestPojo() 方法。这里一切正常。我在建议中使用以下代码获得了正确的注释。
建议方法代码 CacheAspect class 模块 "A":
CacheDelete cacheDeleteAnnotation = getAnnotation((MethodSignature) joinPoint.getSignature(),
CacheDelete.class);
获取注解方法:
private <T extends Annotation> T getAnnotation(MethodSignature methodSignature,
Class<T> annotationClass) {
return methodSignature.getMethod().getAnnotation(annotationClass);
}
问题: 现在我有一个不同的模块 "B",我在其中使用 "A" 的依赖项并且 "B" 模块中的方法之一用 @CacheDelete 注释。
当我 运行 模块 "B" 中的测试用例用于注释方法并调试 CacheAspect class 时,调试点出现在我的建议中,但我得到注释 returns 此处为空。 谁知道可能是什么原因?
得到问题,与不同模块无关。我注释的方法是接口的实现,并通过接口引用变量调用实现的方法。
所以当你使用:
(MethodSignature) proceedingJoinPoint.getSignature().getMethod()
它returns来自接口的方法;
相反,我将上面的代码替换为:
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = signature.getMethod();
String methodName = method.getName();
if (method.getDeclaringClass().isInterface()) {
method = proceedingJoinPoint.getTarget().getClass().getDeclaredMethod(methodName,
method.getParameterTypes());
}
因此,这将检查方法是否为接口,如果是,我将调用:
proceedingJoinPoint.getTarget().getClass().getDeclaredMethod()
这给了我子类的方法。
奇怪的是,当我们通过接口调用子类方法时,当子类方法中使用注解时调用传播到通知(AOP),但注解不会在子类方法中传播。