Spring--context:component-scan扫描详记

springMVC 和 Spring 父子容器对于不一样的类进行管理所出现的冲突。 web

通常的解决方案为,针对 核心业务层的代码和 web层的代码实现分别加载。 spring

这里就涉及到了包扫描的详细内容。 express

如下表示使用默认的过滤规则,扫描 com.aa.bb包及其子包下的全部类 spa

<context:component-scan base-package="com.aa.bb"  />

以上等同于 code

<context:component-scan base-package="com.aa.bb"  use-default-filters="true"/>

如下表示使用默认的过滤规则,扫描 com.aa.bb包及其子包下的全部类,可是排除注解为 Controller的类 component

<context:component-scan base-package="com.aa.bb"  >
	<context:exclude-filter type="annotation" 
			expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

以上等同于 orm

<context:component-scan base-package="com.aa.bb"  use-default-filters="true" >
	<context:exclude-filter type="annotation" 
			expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

映射到实际场景,以上的配置,适合配置Spring容器的扫描规则,即排除掉对注解为Controller的类(大扫描粒度的前提下,排除小粒度), xml

而springMVC所扫描的粒度,应该是只扫描注解为Controller的类,即应该是一种扫描更为精准的粒度: io

<context:component-scan base-package="com.aa.bb"  use-default-filters="false" >
	<context:include-filter type="annotation" 
			expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

以上表示,在com.aa.bb的包及其子包的范围下,只扫描注解为Controller的类 class

相关文章
相关标签/搜索