tags:

views:

105

answers:

1

I'm trying to use Spring AOP to inject behavoir into an object. The target object has a single method which is the join point for this new behavior. That method also has a custom annotation that I want to be able to read from other unrelated code. Because my class doesn't implement an interface, Spring is using CGLIB to dynamically subclass the target object. The generated subclass doesn't seem to have the annotation added to the original class.

I tried adding the @Inherited annotation to the custom annotation without luck. It turns out @Inherited only applies to classes. This suggests that Spring would have to explicitly apply my annotation to the overridden method in the subclass.

EDIT: I found a workaround and some more information on the problem. It looks like the CGLib people are aware of the issue. Annotations are treated like implementation in Java (rather than like part of the method signature). Overriding methods must re-declare annotations. CGLib doesn't do this (intentionally by the sound of things).

My workaround was to change my reflection logic. Where I was searching for the annotated method on the class passed to me, I now look at the class and it's super classes. I end up locating the method using the superclass (in the case of a proxy), but when I call the method, regular polymorphisim applies. This makes for some ugly reflection code (even more ugly than normal). It also seems like I shouldn't have to compensate for a SpringAOP implementation detail in my code.

It seems like Spring should provide a cleaner way to handle this. If anyone has other suggestions, they would be appreciated.

A: 

Are you sure you have the RetentionPolicy on your annotation set to RUNTIME ?

cliff.meyers
Yes. The annotation can be found if my Aspect isn't applied to the class (ie, if there is no CGLib subclass)
Bryan Young