org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factory. 小灰灰 2022-02-25 13:14 255阅读 0赞 `org.springframework.beans.factory.UnsatisfiedDependencyException` `org.springframework.beans.factory.BeanNotOfRequiredTypeException` 异常 1)问题描述: 在进行ssm框架整合的时候,在进行相关的测试代码以及配置文件写完以后,tomcat服务器启动以后,就报了这个异常 `org.springframework.beans.factory.BeanNotOfRequiredTypeException`, 这个是springAOP基于注解的配置时,发生了动态代理的异常,找不到指定的动态代理,属于代理注入的异常。 (2)错误显示: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userServiceImpl' is expected to be of type 'cn.demo.service.impl.UserServiceImpl' but was actually of type 'com.sun.proxy.$Proxy20' at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1348) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:672) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:638) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:686) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:554) at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:499) at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:172) at javax.servlet.GenericServlet.init(GenericServlet.java:158) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1144) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:773) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userServiceImpl' is expected to be of type 'cn.demo.service.impl.UserServiceImpl' but was actually of type 'com.sun.proxy.$Proxy20' at org.springframework.beans.factory.support.DefaultListableBeanFactory.checkBeanNotOfRequiredType(DefaultListableBeanFactory.java:1527) at org.springframework.beans.factory.support.DefaultListableBeanFactory.checkBeanNotOfRequiredType(DefaultListableBeanFactory.java:1534) at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1505) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ... 39 more ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjYxNDA4MA_size_16_color_FFFFFF_t_70] ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjYxNDA4MA_size_16_color_FFFFFF_t_70 1] (3)解决方案: 观察错误的日志,特别是关注于Caused by里面的内容, `Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userServiceImpl' is expected to be of type 'cn.demo.service.impl.UserServiceImpl' but was actually of type 'com.sun.proxy.$Proxy20'` 由这个可以知道,spring的AOP注解配置时动态代理的异常,实现类UserServiceImpl不能使用jdk的动态代理注入,jdk的动态代理不支持类的注入,只支持接口的注入。如果不想使用jdk的接口动态代理,想要使用类的代理,那么就使用cglib的代理,需要引用cglib的依赖。 所以,在知道原因之后,我们就可以这样做, 在spring的配置文件applicationContext.xml中,添加`<aop:aspectj-autoproxy>`,加上属性 `proxy-target-class = “true”`,也就是`<aop:aspectj-autoproxy proxy-target-class = “true”>` `</aop:aspectj-autoproxy>`,这样就是使用了类的动态代理。 解释说明:`proxy-target-class`的属性值决定是基于接口的动态代理还是基于类的代理被创建 `proxy-target-class = true` 当它的属性值设置为true的时候,是使用基于类的动态代理,需要引入cglib的依赖。 `proxy-target-class = false` 当它的属性值设置为false或者是不设置为默认值的时候,是使用jdk的接口的动态代理,标准的jdk的动态代理将会起作用。 (4)总结:分析问题尽量多看Caused by里面的内容。 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjYxNDA4MA_size_16_color_FFFFFF_t_70]: /images/20220225/bc0f3fb25c114eee85ec083294063040.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjYxNDA4MA_size_16_color_FFFFFF_t_70 1]: /images/20220225/576ef56636c44416a4d96ed6361275ba.png
还没有评论,来说两句吧...