一、使用注解配置bean @Controller @Service @Repository
結果:
二、基於xml的屬性裝配
結果:
三、
實驗32:使用context:include-filter指定掃描包時要包含的類
實驗33:使用context:exclude-filter指定掃描包時不包含的類
< context:component-scan base-package ="com.neuedu.ioc.bean"/>
[1]base-package屬性指定一個需要掃描的基類包,Spring容器將會掃描這個基類包及其子包中的所有類。
[2]當需要掃描多個包時可以使用逗號分隔,
[3]如果僅希望掃描特定的類而非基包下的所有類,可使用resource-pattern屬性過濾特定的類,示例:
<context:component-scan base-package="com.neuedu.component" resource-pattern="autowire/*.class"/>
[4]包含與排除
●<context:include-filter>子節點表示要包含的目標類
注意:通常需要與use-default-filters屬性配合使用才能夠達到“僅包含某些組件”這樣的效果。
即:通過將use-default-filters屬性設置為false,禁用默認過濾器,然后掃描的就只是include-filter中的規則
指定的組件了。
●<context:exclude-filter>子節點表示要排除在外的目標類
●component-scan下可以擁有若干個include-filter和exclude-filter子節