org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘top.roo

org.springframework.beans.factory.NoSuchBeanDefinitionException

When I was integrating SSM, I encountered this problem because the project did not find the bean that needed to be injected (this bean will be displayed in the error log). To solve this problem, we need to perform the following troubleshooting steps:

  1. Has this bean been injected into the spring container?
  2. junit performs unit testing to test whether the corresponding data can be obtained from the database
 @Test
 public void test01(){
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //这里可以测试,是否可以拿到相应的bean,即spring中容器中是否有此bean
        BookService bookServiceImpl = (BookService) context.getBean("bookServiceImpl");
        List<Book> bookList = bookServiceImpl.queryAllBooks();
        for (Book book : bookList) {
    
    
            System.out.println(book);
        }
 }
  1. If there are no problems with the above two points, then it means that the problem is not with the underlying layer, but with the spring configuration.
  2. When integrating springMVC, to use our service beans, we must consider the following two points:
    1. The applicationContext.xml file does not integrate beans. Sometimes when we use multiple sub-files to implement spring configuration, we must remember to import the sub-files into the applicationContext.xml file as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="classpath:spring-mvc.xml"/>
    <import resource="classpath:spring-dao.xml"/>
    <import resource="classpath:spring-service.xml"/>
</beans>
  1. Web.xml also requires configuration files. It should be noted that the configuration we associate is applicationContext.xml
    , the overall configuration file of spring , not the sub-file of spring configuration.
<servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

Guess you like

Origin blog.csdn.net/weixin_42164880/article/details/113406984