Why did you fail to create the bean after adding the < context:component-scan > tag to the springmvc.xml file?

in the following Spring mvc code, why did the creation of bean fail? I have added < context:component-scan base-package= "" > to the springmvc.xml file

.

where the project directory structure is

cause of error report:
Caused by:
org.springframework.beans.factory.BeanCreationException:
Could not autowire field:
private com.atguigu.springmvc.UserService com.atguigu.springmvc.HelloWorld.userService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.atguigu.springmvc.UserService] 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 following is the specific code of the file:

web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>
springmvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <context:component-scan base-package="com.atguigu.springmvc"/>
    
    <!--  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
HelloWorld.java

@Controller
public class HelloWorld {

    @Autowired
    private UserService userService;
    
    public HelloWorld() {
        System.out.println("HelloWorld Constructor...");
    }
    
    @RequestMapping("/helloworld")
    public String hello(){
        System.out.println("success");
        return "success";
    }
    
}
UserService.java

@Service
public class UserService {

    @Autowired
    private HelloWorld helloWorld;
    
    public UserService() {
        System.out.println("UserService Constructor...");
    }
    
}
Mar.18,2021

check whether the @ Service comment package in UserService.java is misled. It may lead to org.springframework.stereotype.Service


HelloWorld is Controller,UserService, so why should Controller be injected into Service? it belongs to a recursive call. It will definitely report an error

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e43de7-44a70.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e43de7-44a70.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?