среда, 17 августа 2016 г.

Non-compatible bean instance of type $Proxy123

Situation: I have a @Configuration class, with a @Bean method, which return type is a concrete class, not an interface. And this class is @Transactional.
Ran into an exception:

java.lang.IllegalStateException: @Bean method SomeConfig.beanCreateMethod called as a bean reference for type [com.example.BeanType] but overridden by non-compatible bean instance of type [com.sun.proxy.$Proxy123]. Overriding bean of same name declared in: com.example.SomeConfig

According to this thread (it is old even now; but still useful):

The problem is that you use spring aop at your application (either directly or indirectly via using spring transactions).

Spring aop is proxy-based. It uses jdk proxies by default.

Jdk proxies can be casted only to the target interfaces. If you try to cast a proxy to the concrete class you get ClassCastException.

Bean named 'timedUserSync' is processed by spring aop and every time you try to use it at the application, you use jdk proxy instead. That produces the problem when the framework tries to use the bean as an object of concrete class.

Solution is to setup cglib usage for your aop infrastructure - set 'proxy-target-class="true''' for all used <aop:config>, <aop:aspectj-autoproxy> or <tx:annotation-driven> elements. Also you must be sure that cglib binaries are available at classpath.

Corresponding reference sections:
So adding proxyTargetClass = true to the @EnableTransactionManagement annotation solved the problem.

Комментариев нет:

Отправить комментарий