Hey!!! object.getClass() is misbehaving

If you are using annotations in your code base to configure a generic algorithm, it is very common to have code that looks like:


This code works correctly, except when in the presence of proxies. And, when we factor in that Spring bases their AOP support on the use of proxies, the code like the one mentioned above could give you some head aches.

So what’s the catch ?

If you have a bean defined as:

  <bean id="weirdBean" class="net.weirdtown.WeirdBean">

And then somehow you got a reference to that bean from Spring’s application context.

It won’t be safe to assume that object.getClass() will return the actual object’s class because Spring might have proxied your bean.

To make sure you’ll get the actual object’s class, you’ll have to use



It's only fair to share...
Share on Facebook
Tweet about this on Twitter
Share on LinkedIn

Leave a Reply