An error is reported in the Spring project:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demoController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springdemo.service.DemoDeviceService com.springdemo.controller.DemoController.demoDeviceService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springdemo.service.DemoDeviceService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
The web.xml is configured with:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring.xml,
classpath:spring-hibernate.xml
</param-value>
</context-param>
Tomcat startup log: No Spring WebApplicationInitializer types detected on classpath
But without configuration:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
normal after configuration
Tomcat startup log : Initializing Spring root WebApplicationContext
Spring provides an implementation class of ServletContentListener, the ContextLoaderListener listener, which can be used as a Listener. When the Tomcat container is started, the function of this class is to automatically load the configuration information of the ApplicationContext. If the initial parameter of contextConfigLocation is not set, the default parameter WEB will be used. - application.xml file under the INF path. If you need to customize reading multiple configuration files or modify the default path, you can set it in web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring.xml,
classpath:spring-hibernate.xml
</param-value>
</context-param>
ContextLoaderListener will read these XML files and generate a WebApplicationContext object, and then place this object in the properties of the ServletContext
In this way, as long as we can get the Servlet, we can get the WebApplicationContext object, and use this object to access the beans managed by the spring container.
After the initial success, the display is as follows
信息: Initializing Spring root WebApplicationContext