为什么applicationContext.xml和spring-servlet.xml中都有注解过滤<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.

简介: 在刚学习SpringMVC框架整合时,你也许会产生疑问为什么Spring.xml和SpringMVC.xml中都有注解过滤。 和 在这里有一个比较有意思的问题,初学者也会产生困惑,那么他究竟有何不同?我思考和查询很久,话不多说,也实践做起: (1)只在applicationContext.

在刚学习SpringMVC框架整合时,你也许会产生疑问为什么Spring.xml和SpringMVC.xml中都有注解过滤。

<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.controller" />

在这里有一个比较有意思的问题,初学者也会产生困惑,那么他究竟有何不同?我思考和查询很久,话不多说,也实践做起:

(1)只在applicationContext.xml中配置如下

<context:component-scan base-package="myproject">

启动不报错,,但@Controller失效

 

(2)只在spring-servlet.xml中配置上述配置

 Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource [/WEB-INF/spring-mvc-......
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'commonController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private eis.service.ICollegeServer eis.controller.CommonController.collService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [eis.service.ICollegeServer] 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)}
 

(3)在applicationContext.xml和spring-servlet.xml中都配置过滤<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.controller" />,

不报错,请求正常,

总结:由此可以分析<context:component-scan base-package="myproject">主要过滤@Controller,同时还查资料了解<context:component-scan base-package="myproject.controller" /> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能。

 

目录
相关文章
|
4月前
|
前端开发 JavaScript Java
Web.xml - Servlet与Filter的url-pattern
Web.xml - Servlet与Filter的url-pattern
56 8
|
4月前
|
XML 前端开发 Java
Spring MVC - context:component-scan实现原理与实例
Spring MVC - context:component-scan实现原理与实例
24 0
|
8月前
|
前端开发 Java Maven
项目web.xml中配置DispatcherServlet时,红色波浪线报错(Cannot resolve Servlet ‘DispatcherServlet‘)
项目web.xml中配置DispatcherServlet时,红色波浪线报错(Cannot resolve Servlet ‘DispatcherServlet‘)
|
8月前
|
XML 数据格式
告别web.xml映射Servlet、Filter、Listener,解锁注解新方式开发
告别web.xml映射Servlet、Filter、Listener,解锁注解新方式开发
|
10月前
|
XML 缓存 Java
Spring IOC源码:<context:component-scan>源码详解
Spring IOC源码:<context:component-scan>源码详解
46 0
|
10月前
|
XML JSON JavaScript
JavaWeb总结--XML、Servlet
JavaWeb总结--XML、Servlet
|
XML 数据格式
XML配置方式编写Servlet
XML配置方式编写Servlet
56 0
XML配置方式编写Servlet
|
XML 缓存 Java
Spring IoC源码学习:context:component-scan 节点详解
在 Spring IoC:parseCustomElement详解 中,我们介绍了自定义命名空间节点解析的大部分内容,但是还剩下节点解析的具体过程。本文将以<context:component-scan /> 节点为例子,介绍自定义命名空间 context 的 component-scan 节点的解析过程。
184 0
Spring IoC源码学习:context:component-scan 节点详解
|
应用服务中间件 编译器 Android开发
配置Tomcat时server.xml自动还原问题【Tomcat会删除自己在Servlet.xml 配置的Context元素】
配置Tomcat时server.xml自动还原问题【Tomcat会删除自己在Servlet.xml 配置的Context元素】
114 0
配置Tomcat时server.xml自动还原问题【Tomcat会删除自己在Servlet.xml 配置的Context元素】
|
XML Java 数据格式
Spring里component-scan的工作原理
Spring里component-scan的工作原理
Spring里component-scan的工作原理