首页 > 技术文章 > Spring-IoC-DI-基于注解方式的依赖注入(组件扫描配置)

orzjiangxiaoyu 2020-08-16 20:49 原文

①不扫描哪些:context:exclude-filter  

扫描的时候可以排除一些不要的组件  
type="annotation":指定排除规则,按照注解进行排除。标注了指定注解的组件不要扫描  
type="assignable":指定排除具体的某个类,按照类排除  
expression="":指定注解的全类名  

<context:component-scan base-package="com.orz.spring" >
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>

 

②只扫描哪些:context:include-filter

Spring默认都是全部扫描,需要配置 use-default-filters="false" 禁用默认规则才能使得context:include-filter生效

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

 

推荐阅读