2019独角兽企业重金招聘Python工程师标准>>>

背景

Springboot项目在启动时,控制台输出日志:

2019-05-28 15:28:32.439 INFO  main [org.springframework.data.repository.config.RepositoryConfigurationDelegate:165]-Multiple Spring Data modules found, entering strict repository configuration mode!
2019-05-28 15:28:32.596 INFO  main [org.springframework.data.repository.config.RepositoryConfigurationDelegate:165]-Multiple Spring Data modules found, entering strict repository configuration mode!

虽说是INFO级别,但是凭白输出个带“!”的提醒信息还是让人不免好奇,同时更多的会顾及这个提醒是不是架构的一些不稳定的因素。为此笔者决定一探究竟!

分析

  • 根据控制台报文可以看到,日志是从RepositoryConfigurationDelegate类中打印出来的,而根据类路径名称大概知道类是归属spring-data相关包,用IDEA很容易定位到该类是在spring-data-commons包中。
  • 该提示信息输出了两遍,莫非是包存在引用冲突??打开pom.xml文件,查看依赖图 spring-data-commons包并未有依赖冲突。那只好看RepositoryConfigurationDelegate具体实现了。
  • spring-data-commons.jar项目中使用的是1.13.10.RELEASE版本。为了查看方便,找到spring官网提供的github源码地址,并fork,最后本地导入spring-data-commons工程。 https://github.com/spring-projects/spring-data-commons.git RepositoryConfigurationDelegate类的multipleStoresDetected方法打印出了该提示信息。
    private static final String MULTIPLE_MODULES = "Multiple Spring Data modules found, entering strict repository configuration mode!";/*** 扫描jar,找出RepositoryFactorySupport接口的实现类,如果RepositoryFactorySupport接口实现类不止一个则打印提示信息“MULTIPLE_MODULES”。* * Scans {@code repository.support} packages for implementations of {@link RepositoryFactorySupport}. Finding more* than a single type is considered a multi-store configuration scenario which will trigger stricter repository* scanning.** @return*/private boolean multipleStoresDetected() {boolean multipleModulesFound = SpringFactoriesLoader.loadFactoryNames(RepositoryFactorySupport.class, resourceLoader.getClassLoader()).size() > 1;if (multipleModulesFound) {LOG.info(MULTIPLE_MODULES);}return multipleModulesFound;}

方法中SpringFactoriesLoader.loadFactoryNames(RepositoryFactorySupport.class, resourceLoader.getClassLoader()) 是spring.factories机制的重要实现,会查找所有jar包中META-INF/spring.factories文件配置信息。通过对loadFactoryNames方法断点调试,最终确定了在spring-boot-starter-data-redis和spring-boot-starter-data-mongodb相关依赖包中都配置了RepositoryFactorySupport接口实现类。 spring-data-keyvalue.jar

org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory

spring-data-mongo.jar

org.springframework.data.web.config.SpringDataJacksonModules=org.springframework.data.mongodb.core.GeoJsonConfiguration
org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.mongodb.repository.support.MongoRepositoryFactory
  • KeyValueRepositoryFactory和MongoRepositoryFactory作为RepositoryFactorySupport接口的多个实现类在设计上并没有什么问题。 ==但是对Repository功能需要特别注意了== 如果没有使用Repository功能,那么可以通过配置application.yml属性关闭repository功能,如下:
spring:data: mongodb: repositories: enabled: falseredis:repositories:enabled: false

或者启动类中排除repository自动装载,如下:

@SpringBootApplication(exclude = { RedisRepositoriesAutoConfiguration.class, MongoRepositoriesAutoConfiguration.class })

此处列举的是redis和mongo,其他jpa、es等配置都是类似的。关闭了Repository功能后,控制就不会在打印出Multiple Spring Data modules found, entering strict repository configuration mode!的提示信息了。

如果项目中使用了Repository功能,那建议启动类中指定Repository具体包扫描路径,如下:

@EnableMongoRepositories(basePackages = "xxx.mongo.dao")
@EnableRedisRepositories(basePackages = "xxx.redis.dao")

如果不指定具体Repository扫面包路径,那么不仅仅会出现本文标题那段提示信息,Repository覆盖提示信息,如下:

Spring Data Redis - Could not safely identify store assignment for repository candidate interface com.lucas.device.repository.mongo.DeviceInfoRepository.

消息级别仍然是INFO级别,意思是Redis定义的Repository会跳过DeviceInfoRepository等其他继承MongoRepository接口的实例化对象。当我们定义的Repository接口越多,这种提示信息也就会越多。

总结

虽然只是INFO级别的信息,但是还是要摸清楚产生的原由,一步步分析下来,我们更加清楚架构设计不仅仅是使用,更应该以塑造的目光去精雕细琢,码农也可以成功手艺精湛的手艺人。

转载于:https://my.oschina.net/u/872813/blog/3058787

Multiple Spring Data modules found, entering strict repository configuration mode!相关推荐

  1. Spring Data JPA教程,第一部分: Configuration(翻译)

    Spring Data JPA项目旨在简化基于仓库的JPA的创建并减少与数据库交互的所需的代码量.本人在自己的工作和个人爱好项目中已经使用一段时间,它却是是事情如此简单和清洗,现在是时候与你分享我的知 ...

  2. 【转】Spring Data JDBC - Reference Documentation

    Spring Data JDBC - Reference Documentation Jens Schauder, Jay Bryant, Mark Paluch, Bastian Wilhelm V ...

  3. 02 | Spring Data Common 之 Repository 如何全面掌握?

    通过上一课时,我们知道了 Spring Data 对整个数据操作做了很好的封装,其中 Spring Data Common 定义了很多公用的接口和一些相对数据操作的公共实现(如分页排序.结果映射.Au ...

  4. Spring Data Commons 官方文档学习

    Spring Data Commons 官方文档学习   -by LarryZeal Version 1.12.6.Release, 2017-07-27 为知笔记版本在这里,带格式. Table o ...

  5. Spring Data JPA 实战

    课程介绍 <Spring Data JPA 实战>内容是基于作者学习和工作中实践的总结和升华,有一句经典的话:"现在的开发人员是站在巨人的肩上,弯道超车".因现在框架越 ...

  6. SpringBoot学习笔记:Spring Data Jpa的使用

    更多请关注公众号 Spring Data Jpa 简介 JPA JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化规范(JSR ...

  7. Spring Data JPA 教程(翻译)

    写那些数据挖掘之类的博文 写的比较累了,现在翻译一下关于spring data jpa的文章,觉得轻松多了. 翻译正文: 你有木有注意到,使用Java持久化的API的数据访问代码包含了很多不必要的模式 ...

  8. 使用Spring Data MongoDB和Spring Boot进行数据聚合

    MongoDB聚合框架旨在对文档进行分组并将其转换为聚合结果. 聚合查询包括定义将在管道中执行的几个阶段. 如果您对有关该框架的更深入的细节感兴趣,那么 mongodb docs是一个很好的起点. 这 ...

  9. Spring Data JPA的持久层

    1.概述 本文将重点介绍Spring 3.1,JPA和Spring Data的持久层的配置和实现. 有关使用基于Java的配置和项目的基本Maven pom设置Spring上下文的分步介绍,请参阅本文 ...

最新文章

  1. python【蓝桥杯vip练习题库】ADV-290成绩排序
  2. ssm商务会员管理系统_会员管理商城开发
  3. docker mysql 日志_docker中将MySQL运行在容器中失败提示“ InnoDB : Error 22 with aio_write”的解决办法...
  4. Matlab Command Window 进度提示
  5. 前端学习(2282)全选和反选问题
  6. linux安装与登录
  7. linkedin android,如何在android中登录linkedin?
  8. IDEA格式化js代码
  9. Android内购+IM
  10. JDK源码解析---Short
  11. android日历提醒小程序源码,微信小程序倒班日历简洁实用demo完整源码
  12. Ubuntu 开机显示 initramfs 进不了系统
  13. ffmpeg h264文件转mp4
  14. 树莓派4 街机 卡顿_建立开放式街机的4个项目
  15. 自研P2P镜像分发系统
  16. 测控系统原理与设计(1)
  17. 偷梁换柱 暗渡陈仓 一招搞定360安全卫士无法启动
  18. [HTML] 做个空壳网页练手(菜鸡的自我信息完善
  19. ambari集群安装hdp
  20. 中国研发独立移动操作系统 你怎么看?

热门文章

  1. 【朱-刘算法】【最小树形图】hdu6141 I am your Father!
  2. nginx配置一、二级域名、多域名对应(api接口、前端网站、后台管理网站)
  3. 太多选择——企业如何选择合适的BI工具?
  4. TCP及IP报头及协议
  5. bash魔法堂:History用法详解
  6. MySQL事物系列:1:事物简介
  7. 为自己给操作系统留个后门?我的安全管理经验谈
  8. 使用ReaderWriterLock优化文件缓存
  9. 字符串一:替换空格()
  10. AI顶级会议以及期刊