为什么applicationContext.xml和spring-servlet.xml中都有注解过滤<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.
小编 2026-07-03 阅读:581 评论:0在刚学习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中配置上述配置
(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 定义的功能,同时还启用了注释驱动自动注入的功能。
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。


