0
我有豆這樣的:春BeanNotOfRequiredTypeException
<bean id="myBean" class="com.mypackage.MyClass" scope="session">
</bean>
這裏是類聲明:
MyClass extends MySuperClass implements MyInterface<A>
MySuperClass extends GenericClass<A>
後來我嘗試這樣做:
applicationContext.getBean("myBean", GenericClass.class);
而且我得到這個錯誤:
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'myBean' must be of type [com.mypackage.GenericClass], but was actually of type [com.sun.proxy.$Proxy118]
我解決它通過添加:
<aop:scoped-proxy />
我的豆聲明中,但我想了解我在做什麼,在這種情況下,我不知道。
你能解釋一下爲什麼我得到這個例外,爲什麼加入<aop:scoped-proxy />
解決了它?
謝謝!
如果您從bean定義中刪除'scope =「session」',那麼您也不會得到該異常。 –
是的,但是我需要這個bean作爲會話作用域 – jpprade
然後,當然,你需要' ',因爲在普通的'applicationContext.getBean'中沒有HTTP會話來綁定你的bean 。 –