Reputation: 149
I'm working on a java web application that uses the spring framework. The application has a class called ServiceA class as shown below. And Interface1Impl implements Interface1 and extends Dao2 class. In Servicea.do() method, casting x to Dao2 throws an exception saying "Failed to convert property value of type [$Proxy1] to required type [Dao2]" How can I fix this so that x can be cast to Dao2? Thanks.
public class ServiceA
{
private final Interface1 x; // injected
public ServiceA(Interface1 aInterface1Impl)
{
x = aInterface1Impl;
}
public string do()
{
// Exception: Failed to convert property value of type [$Proxy1]
// to required type [Dao2]
Dao2 dao = (Dao2)x;
return dao.run();
}
}
Here's the partial spring config file
<bean id="dao-t" class="Interface1Impl">
<property name="ibatis" ref="ibatis-main"/>
</bean>
<bean id="proj" class="ServiceA">
<constructor-arg ref="dao-t"/>
</bean>
Upvotes: 1
Views: 4560
Reputation: 597124
The best option is to define the run()
method in the interface.
A less preferable option is to specify proxy-target-class="true"
to your transactional aspect (or whatever makes proxies around your objects)
The reason this does not work is that spring has created a proxy by interface, and the class is used within the invocation handler. So the proxy implements the interface, but does not extend the class, and hence you can't cast to it.
Upvotes: 2