为什么有时我们会单独声明context:component-scan
<ctx:component-scan base-package="com.*">
<ctx:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</ctx:component-scan>
我们声明如下:
<context:component-scan base-package="com.*.*.controller.*" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
为什么我们不在applicationContext.xml中声明如下:
<context:component-scan base-package="com.*"/>
然后将检测所有注释@ Controller,@ Service,@ Repository的组件
答案 0 :(得分:1)
这种分离旨在允许定义多个调度程序servlet,并重用核心业务组件。
通常,您将拥有在application-context.xml
中定义的存储库和服务,并绑定到整个servlet上下文(通过使用ContextLoaderListener
),并定义一个或多个调度程序servlet,每个使用自己的(子)上下文(由控制器和mvc实用程序组件组成),它们将重用父上下文中的相同bean。
我会说即使您有一个调度程序,这种分离也是有意义的,因为它迫使您将“核心域”与应用程序层隔离开来。