spring的<context:component-scan >使用说明

1. 作用

  在xml中配置此标签后, spring可以自动去扫描base-pack下的java文件, 如果扫描到被@Controller,@Service, @Component等注解修饰的类, 则把这些类注册为bean.
 如果配置了 <context:componentscan> <script type="math/tex" id="MathJax-Element-20"> </script>那么 <context:annotationconfig> <script type="math/tex" id="MathJax-Element-21"> </script>标签句就可以不用配置了, 因为前者包含了后者.

2. 两个子标签

<context:includefilter> <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:componentscan> <script type="math/tex" id="MathJax-Element-4"> </script>标签上加上属性: use-default-filter=”false”即可.

<context:excludefilter> <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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值