Donnerstag, 18. September 2014

Why Spring makes me sick #16

In a project I have to work on, there is amongst others a definition of a simple bean of class testDoubles.MemoryLogClient.

So I was expecting that if I retrieved that bean from the Spring application context, that it is of type testDoubles.MemoryLogClient.

But Spring actually replaces the bean by a proxy (the retrieved bean is of type com.sun.proxy.$Proxy) and to make things worse, overrides the toString() method so that it reports type testDoubles.MemoryLogClient.
I.e. in my debug logs I see that the object reports to be of the correct type, but an assignment to a variable of the same type fails with a ClassCastException.

Yet another way Spring makes a developers life really hard...

Oh, by the way, the IMHO simplest solution is to extract an interface (in my case from testDoubles.MemoryLogClient) and assign the retrieved bean to a variable that has this interface as type.
But you will find much more complicated solutions on the interwebs (such as stackoverflow.com) that suggest to use AOP, which will convert this problem into a full blown disaster ;)

Keine Kommentare:

Kommentar veröffentlichen