1. 作用
在xml中配置此标签后, spring可以自动去扫描base-pack下的java文件, 如果扫描到被@Controller,@Service, @Component等注解修饰的类, 则把这些类注册为bean.
如果配置了
<context:component−scan>
<script type="math/tex" id="MathJax-Element-20">
</script>那么
<context:annotation−config>
<script type="math/tex" id="MathJax-Element-21">
</script>标签句就可以不用配置了, 因为前者包含了后者.
2. 两个子标签
①
<context:include−filter>
<script type="math/tex" id="MathJax-Element-3">
</script>
解释: 只扫描指定的注解.
<!-- 代码片段1 -->
<!-- 精确到一个包,include-filter生效, 只扫描@Controller -->
<context:component-scan base-package="online.bendou.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 代码片段2 -->
<!-- 包括很多包(包不精确),include-filter失效, 还是扫描全部注解 -->
<context:component-scan base-package="online.bendou">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
发现: 如果包的路径不是具体到某一个包, 而使用了include-filter的话, 它会失效(还是扫描全部注解), 而且也会使得事务不起作用. 解决:在 <context:component−scan> <script type="math/tex" id="MathJax-Element-4"> </script>标签上加上属性: use-default-filter=”false”即可.
②
<context:exclude−filter>
<script type="math/tex" id="MathJax-Element-5">
</script>
解释: 不扫描指定的注解.
<!-- 代码片段3 -->
<!-- 不扫描@Controller注解, 此处包的路径是否精确对"排除"注解是没影响的 -->
<context:component-scan base-package="online.bendou">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>