Spring 扫描标签<context:component-scan/>

一. <context:annotation-config/>

此标签支持一些注入属性的注解, 列如:@Autowired, @Resource注解

 

二. <context:component-scan/>

几个关键属性

1. use-default-filters: 默认为true, 支持@Controller, @Service, @Repository, @Component注解

2. annotation-config: 默认为true, 相当于开启<context:annotation-config/>标签

3. base-package: 指定扫描包的路径, 包括子包

 

两个子标签

1. <context:exclude-filter/>: 表示排除

2. <context:include-filter/>: 表示增加

常用配置

<!-- springmvc -->

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

<!-- spring -->

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

表示springmvc只扫描com.waston.controller及其子包, 并且只扫描@Controller注解

spring扫描com.waston及其子包, 包括com.waston.controller, 但是不扫描@Controller注解

注:use-default-filters="false"必须要加, 否则默认为true, 也就是说会扫描上面说的四个注解,那么

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>就不起作用了.

posted on 2018-02-01 23:26  wastonl  阅读(232)  评论(0)    收藏  举报