2011-03-24 116 views
1

我正在使用使用spring框架的java web應用程序。 該應用程序有一個名爲ServiceA類的類,如下所示。 而Interface1Impl實現了Interface1並擴展了Dao2類。 在Servicea.do()方法中,將x強制轉換爲Dao2會拋出異常,指出「無法將類型[$ Proxy1]的屬性值轉換爲所需類型[Dao2]」 如何解決此問題,以便x可以轉換爲Dao2 ?謝謝。

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(); 
    } 
} 

這裏的局部彈簧配置文件

<bean id="dao-t" class="Interface1Impl"> 
    <property name="ibatis" ref="ibatis-main"/> 
</bean> 

<bean id="proj" class="ServiceA"> 
    <constructor-arg ref="dao-t"/> 
</bean> 

回答

2

最好的選擇是,以限定在界面中run()方法。

一個不太可取的選擇是指定proxy-target-class="true"到您的事務方面(或任何使代理人在你的對象)

這不工作的原因是,春天已經創造了通過接口的代理,並使用類在調用處理程序中。所以代理實現了接口,但不擴展這個類,因此你不能投射它。

相關問題