欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

The bean 'llWebSocketHandler' could not be injected because it is a JDK dynamic proxy that implemen

程序员文章站 2024-01-31 08:30:46
...

摘要:在做Spring Boot、WebSockets整合的时候,出现了bean注入失败的问题,错误异常如下:

一:异常信息:

2018-08-10 11:44:50.072  WARN 20296 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'llWebSocketConfig': Unsatisfied dependency expressed through field 'llWebSocketHandler'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'llWebSocketHandler' is expected to be of type 'com.micai.spring.messaging.web.socket.LlWebSocketHandler' but was actually of type 'com.sun.proxy.$Proxy57'
2018-08-10 11:44:50.075  INFO 20296 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-08-10 11:44:50.287  INFO 20296 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-08-10 11:44:50.292 ERROR 20296 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'llWebSocketHandler' could not be injected as a 'com.micai.spring.messaging.web.socket.LlWebSocketHandler' because it is a JDK dynamic proxy that implements:


Action:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.

其中最重要的一句就是:

The bean 'llWebSocketHandler' could not be injected as a 'com.micai.spring.messaging.web.socket.LlWebSocketHandler' because it is a JDK dynamic proxy that implements:

二:解决过程:

我们知道:spring.proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。首先说明下proxy-target-class="true"和proxy-target-class="false"的区别,为true则是基于类的代理将起作用(需要cglib库),为false或者省略这个属性,则标准的JDK 基于接口的代理将起作用。
proxy-target-class在spring事务、aop、缓存这几块都有设置,其作用都是一样的。

所以要想解决上面的问题,只需要在application.properties文件中配置下spring.aop.proxy-target-class=true就行,这样LlWebSocketHandler类就可以被当做基于类的代理注入进来了。

以上就是对这个错误的分析过程。