spring配置<context:component-scan>

spring配置<context:component-scan>

在xml配置了这个标签后,spring可以自动去扫描base-package指定包及子包下面的java文件,如果扫描到有@Component注解的类,则把这些类注册为bean
该标签有两个子标签:<context:include-filter>、<context:exclude-filter>

@Component注解有多个子注解,如@Controller, @Service, @Reposity等。
我们经常把不同层次的bean分在不同的配置文件中配置,如,通常controller组件会单独配置在《spring-dispatcher.xml》中,

上面两个子标签是用来做这种过滤的。

但是首先,<context:component-scan>有一个use-default-filters属性,默认值为true,
如果要使上面的子标签过滤生效,应该先将这个属性值改为false。

如下面的配置,只扫描带有@Controller注解的类

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

 

posted @ 2017-03-14 14:50  LinuSiyu  阅读(228)  评论(0编辑  收藏  举报