Multiple Spring Data modules found, entering strict repository configuration mode!

背景

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文件,查看依赖图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
    spring-data-commons
    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: false
        redis:
            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级别的信息,但是还是要摸清楚产生的原由,一步步分析下来,我们更加清楚架构设计不仅仅是使用,更应该以塑造的目光去精雕细琢,码农也可以成功手艺精湛的手艺人。

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值