3

I have an interface annotated with @Transactional. Then concrete classes implementing that interface. Because of the annotation spring creates a proxy for every class implementing the interface.

My problem is that at container boot-up time I am checking if some of the classes are annotated with a specific user custom type annotation (if not I am throwing an exception). It seems that getAnnotation() method on the proxy returns null.

Is the proxy not supposed to contain all the attributes(e.g annotations) associated with the proxied class?

4

1 回答 1

10

Spring 创建代理,默认情况下 JDK 动态代理,它基本上创建一个动态类作为启动时间(那些漂亮的 $Proxy42 类),它就像你的接口实例一样。如果您现在调用 get class,您将获得动态创建的类。

使用AopProxyUtilsspring 中的实用程序类来获取包含注释的实际类。

所以而不是

Class<?> clazz = someObject.getClass();

Class<?> clazz = AopProxyUtils.ultimateTargetClass(someObject);

那应该给你实际的(包装的)类。

于 2013-10-02T12:26:14.220 回答