Вопрос

I have bean like this :

<bean id="myBean" class="com.mypackage.MyClass" scope="session">
</bean>

here is the class declaration :

MyClass extends MySuperClass implements MyInterface<A>


MySuperClass extends GenericClass<A>

Later I try to do this :

 applicationContext.getBean("myBean", GenericClass.class);

And I get this error :

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'myBean' must be of type [com.mypackage.GenericClass], but was actually of type [com.sun.proxy.$Proxy118]

I solved it by adding :

<aop:scoped-proxy />

inside my bean declaration but I like to understand what I am doing and in this case I don't.

Could you explain me why I am getting this exception and why adding <aop:scoped-proxy /> solved it ?

thanks !

Это было полезно?

Решение

By adding <aop:scoped-proxy/> I believe you are telling Spring to use a smart object proxy rather than just a plain JDK interface proxy. Basically your proxy object is backed by your actual object so that when you pass it around it looks just like your regular object. It sounds like a compromise between using AspectJ loadtime weaving and plain JDK proxies. Have a read of what the equivalent annotation does here

EDIT 1: Actually it looks like using CGLib (AspectJ) based proxies is the default option there. Thats probably what fixed your problem.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top