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

相關文章
相關標籤/搜索