You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The text was updated successfully, but these errors were encountered:
vergilyn
changed the title
由于NacosConfigApplicationContextInitializer中获取的是NacosConfigLoader单例对象,当@SpringBootTest创建多个spring-context无法正确获取nacos中配置的属性
当@SpringBootTest创建多个spring-context时,无法正确获取nacos中配置的属性
Sep 5, 2024
spring-boot:
2.7.18
nacos-config-spring-boot-autoconfigure:
0.2.12
问题描述
application.properties
配置示例:当使用
@SpringBootTest
进行单元测试时,存在例如@SpyBean
或者@MockBean
时,会多次创建spring.context
(对应多个spring.environment
)。(参考:https://github.com/spring-projects/spring-boot/issues/34768)但是,只有 第1次创建的
spring.context
中可以获取com.xxx.properties
中配置的属性。第2+次创建的
spring.context
中都无法正确获取com.xxx.properties
中的属性。可能原因
由于
NacosConfigApplicationContextInitializer
中获取的是单例NacosConfigLoader
,并且NacosConfigLoader
存在对spring.environment
的引用。当多次创建
spring.context
时,NacosConfigLoader
引用的始终是第1次创建的spring.environment
,导致第2+次无法正确将nacos中的配置项写入spring.environment
,从而无法正确创建spring.context
。nacos-spring-boot-project/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/autoconfigure/NacosConfigApplicationContextInitializer.java
Lines 71 to 74 in eaf15a6
nacos-spring-boot-project/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/util/NacosConfigLoaderFactory.java
Lines 30 to 44 in eaf15a6
期望
多次创建
spring.context
时,能正确将nacos的配置项写入到对应的spring.environment
中The text was updated successfully, but these errors were encountered: