前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 ><context:component-scan> 配置 —— 分库遇到问题(1)

<context:component-scan> 配置 —— 分库遇到问题(1)

作者头像
日薪月亿
发布2019-05-14 14:42:23
9170
发布2019-05-14 14:42:23
举报
文章被收录于专栏:技术探索技术探索

项目中 springMvc的部分配置如下:

代码语言:javascript
复制
<dubbo:annotation />
<context:component-scan base-package="xxx">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController" />
</context:component-scan>

相信有些人看到我贴出来的配置就知道我要说明什么问题了,如果你还是没有头绪的话,可以看下我遇到的问题。 这个配置文件本来是想要扫描 xxx包下面的Controller和 RestControl注解,看起来并没有什么问题。我无意中一次测试发现某些service被初始化了两次!这跟spring中的单例模式是相悖的。并且一个service在spring根容器和springMvc容器分别初始化一次,导致在根容器初始化的Service里面的dubbo的 @Reference无法注入。 于是开始排查错误:除了dubbo:annotation是本人加的,其他的配置都是已经存在的。当时知道肯定是配置文件出了问题,但是不知道具体是哪里。问了公司的其他人员还是没有找到根本原因,经过反复排除并且在spring的官方文档发现了问题的根源。

spring官方给的解释如下:

The following example shows the configuration ignoring all @Repository annotations and using “stub” repositories instead. @Configuration @ComponentScan(basePackages = "org.example", includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"), excludeFilters = @Filter(Repository.class)) public class AppConfig { ... }

and the equivalent using XML

代码语言:javascript
复制
<beans>
    <context:component-scan base-package="org.example">
        <context:include-filter type="regex"
                expression=".*Stub.*Repository"/>
        <context:exclude-filter type="annotation"
                expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
</beans>

[Note] You can also disable the default filters by setting useDefaultFilters=false on the annotation or providing use-default-filters=”false” as an attribute of the element. This will in effect disable automatic detection of classes annotated with @Component, @Repository, @Service, @Controller, or @Configuration.

所以通过官方的描述可以看出原来是使用错误导致的。也就是说如果想让项目中的context:include-filter/生效就必须要加use-default-filters=”false” 否则 spring还是会扫描包下面的以下注解 @Component, @Repository, @Service, @Controller, or @Configuration.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-07-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • spring官方给的解释如下:
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档