首页 新闻 赞助 找找看

为什么在springmvc.xml文件中添加<context:component-scan>标签后,还创建bean失败

0
悬赏园豆:50 [待解决问题]

下面的Spring mvc代码中,为什么创建bean失败,我已经在springmvc.xml文件中添加<context:component-scan base-package="">了啊

其中,项目目录结构为:

 

项目报错原因:

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)}

下面是文件的具体代码:

其中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...");
    }
    
}

 

秋风秋雨123的主页 秋风秋雨123 | 初学一级 | 园豆:51
提问于:2018-06-10 21:58
< >
分享
所有回答(2)
0

service 一般都是注入接口的吧

让我发会呆 | 园豆:2929 (老鸟四级) | 2018-06-11 08:44

而spring默认是JDK动态代理,对实现类对象做增强得到的增强类与实现类是兄弟关系,所以不能用实现类接收增强类对象,只能用接口接收。

支持(0) 反对(0) 让我发会呆 | 园豆:2929 (老鸟四级) | 2018-06-11 08:47
0

糯糯的问一句,HelloWorld实例化查找UserService,UserService实例化时查找HelloWorld,什么时候能实例化完成?你不觉得这是个死循环么?

夜半风起 | 园豆:224 (菜鸟二级) | 2018-06-15 16:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册