Spring MVC:Error creating bean with name 'userController': Injection of resource dependencies

22:48:34.327 [RMI TCP Connection(3)-127.0.0.1] ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.dcits.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:325) ~[spring-context-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:847) ~[spring-beans-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:530) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170) ~[spring-webmvc-5.1.11.RELEASE.jar:5.1.11.RELEASE]
    at javax.servlet.GenericServlet.init(GenericServlet.java:158) ~[servlet-api.jar:4.0.FR]
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1174) ~[catalina.jar:9.0.2]
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1090) ~[catalina.jar:9.0.2]
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:980) ~[catalina.jar:9.0.2]
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4829) ~[catalina.jar:9.0.2]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5139) ~[catalina.jar:9.0.2]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[catalina.jar:9.0.2]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:740) ~[catalina.jar:9.0.2]
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:716) ~[catalina.jar:9.0.2]
..............

问题原因:注入 bean 失败异常,查看对应Bean的注解没有问题。最后排查发现是web.xml配置出了问题

原web.xml配置:

Spring MVC:Error creating bean with name 'userController': Injection of resource dependencies_第1张图片

更改后:

Spring MVC:Error creating bean with name 'userController': Injection of resource dependencies_第2张图片

原因:

ContextLoaderListener实现了ServletContextListener接口,ServletContextListener是Java EE标准接口之一,tomcat容器启动时便会触发该接口的contextInitialized。其中 obtainFreshBeanFactory 调用了AbstractRefreshableApplicationContext类的refreshBeanFactory方法。而refreshBeanFactory调用了XmlWebApplicationContext的loadBeanDefinitions

所以在启动Tomcat服务初始化Controller的javaBean时候会报Error creating bean with name 'userController': Injection of resource dependencies failed。在web.xml中增加


    org.springframework.web.context.ContextLoaderListener

后重启启动后问题解决。

你可能感兴趣的:(Spring,java,spring)