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

本文深入探讨了SpringBoot项目启动时出现的MultipleSpringDatamodulesfound警告信息,详细分析了RepositoryConfigurationDelegate类的工作原理,解释了如何通过配置避免多数据模块引起的Repository功能冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

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

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

2025.03.17 10:09:33|INFO |org.springframework.boot.SpringApplication|logStartupProfileInfo()|655|The following profiles are active: dev 2025.03.17 10:09:35|INFO |org.springframework.data.repository.config.RepositoryConfigurationDelegate|multipleStoresDetected()|249|Multiple Spring Data modules found, entering strict repository configuration mode! 2025.03.17 10:09:35|INFO |org.springframework.data.repository.config.RepositoryConfigurationDelegate|registerRepositoriesIn()|127|Bootstrapping Spring Data Redis repositories in DEFAULT mode. 2025.03.17 10:09:35|INFO |org.springframework.data.repository.config.RepositoryConfigurationDelegate|registerRepositoriesIn()|187|Finished Spring Data repository scanning in 148ms. Found 0 Redis repository interfaces. 2025.03.17 10:09:35|INFO |org.springframework.cloud.context.scope.GenericScope|setSerializationId()|295|BeanFactory id=583b7bc4-7db1-3dd1-be46-5c61142142a7 2025.03.17 10:09:35|INFO |org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker|postProcessAfterInitialization()|330|Bean 'com.arpa.wms.api.EnzomesaServerAPI' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025.03.17 10:09:35|INFO |org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker|postProcessAfterInitialization()|330|Bean 'threadConfig' of type [com.arpa.wms.config.ThreadConfig$$EnhancerBySpringCGLIB$$bcdc659] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025.03.17 10:09:35|INFO |org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker|postProcessAfterInitialization()|330|Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025.03.17 10:09:36|INFO |org.springframework.boot.web.embedded.tomcat.TomcatWebServer|initialize()|92|Tomcat initialized with port(s): 8080 (http) 2025.03.17 10:09:36|INFO |org.apache.juli.logging.DirectJDKLog|log()|173|Initializing ProtocolHandler ["http-nio-8080"] 2025.03.17 10:09:36|INFO |org.apache.juli.logging.DirectJDKLog|log()|173|Starting service [Tomcat]
03-18
2025-03-18 15:21:20.952 INFO 14980 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$d6fefe4a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.12.RELEASE) 2025-03-18 15:21:21.974 INFO 14980 --- [ main] com.yirui.app.AppApplication : The following profiles are active: prod 2025-03-18 15:21:23.843 INFO 14980 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode! 2025-03-18 15:21:23.845 INFO 14980 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode. 2025-03-18 15:21:23.879 INFO 14980 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 24ms. Found 0 Redis repository interfaces. 2025-03-18 15:21:24.300 INFO 14980 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=e951639c-ce61-38dc-a56a-bb9e08f4d9ff 2025-03-18 15:21:24.330 INFO 14980 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'asyncConfig' of type [com.yirui.core.config.common.AsyncConfig$$EnhancerBySpringCGLIB$$617adaef] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025-03-18 15:21:24.544 INFO 14980 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.datasource.dynamic-com.baomidou.dynamic.datasource
最新发布
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值