Problem:
I am suddenly getting an error after some spring config was changed:
java.lang.ClassCastException: $Proxy47 cannot be cast to com.mycompany.MyConcreteClass
The code looks like this:
MyConcreteClass myClass = (MyConcreteClass) model.get("myClass");
I should add that MyConcreteClass was autoWired with spring elsewhere, and this setup used to work until something was changed in spring config.
Solution:
This is happening because we are casting to the concrete implementation, what needs to be done is that the object needs to be cast to the interface:
MyInterface myClass = (MyInterface) model.get("myClass");
Also, the relevant spring configuration that causes this issue is:
<tx:annotation-driven manager="springTransactionManager" ></tx:annotation-driven>
In order for the proxy to not proxy the interfaces but concrete classes, you need to add the following attribute to the above directive:
proxy-target-class="true"