问题描述
我正在使用spring-boot 2+,并创建了一些自定义注释;
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation{
}
这样做时:
final AnnotatedType[] annotatedTypes = mostSpecificMethod.getAnnotatedParameterTypes();
//this will get the original class
//final Class<?> clazz = AopProxyUtils.ultimateTargetClass(bean);
Class<?> annotatedMappedClass = null;
for (AnnotatedType annotatedType : annotatedTypes) {
if (annotatedType.isAnnotationPresent(MyCustomAnnotation.class)) {
annotatedMappedClass = TypeFactory.rawClass(annotatedType.getType());
}
}
当bean不是代理服务器时它可以工作,但是当我添加@Transactional批注时,它将成为代理服务器并停止工作。在目标类中找到什么Spring Util?
解决方法
据我了解,您将需要bean。使用:
Method invocableMethod = AopUtils.selectInvocableMethod(mostSpecificMethod,bean.getClass());
似乎可以正常工作。
也更复杂的一个:
Method method = mostSpecificMethod;
if (AopUtils.isAopProxy(bean)) {
try {
Class<?> clazz = AopProxyUtils.ultimateTargetClass(bean);
method = clazz.getMethod(mostSpecificMethod.getName(),mostSpecificMethod.getParameterTypes());
}
catch (SecurityException ex) {
ReflectionUtils.handleReflectionException(ex);
}
catch (NoSuchMethodException ex) {
throw new IllegalStateException("...",ex);
}
}