Skip to content

Commit 3a7da96

Browse files
fix: resolve config refresh failure with spring.config.import on 2025.0.x
Fix two defects in NacosPropertySourceRefreshListener that prevented dynamic config refresh when using spring.config.import=nacos: 1. PropertySource name mismatch: ConfigData path uses 'group@dataId' naming while RefreshListener looked for 'dataId,group'. Now tries both naming conventions. 2. File extension hardcoded: RefreshListener always used 'properties' extension, breaking yml/json/xml configs. Now reads actual suffix from NacosPropertySource. Changes: - Add suffix field to NacosPropertySource to preserve file extension - Update NacosPropertySourceBuilder and NacosConfigDataLoader to pass suffix - Update NacosPropertySourceRefreshListener to handle both naming formats and use actual file extension Fixes #4331 Signed-off-by: wushiyuan <wushiyuanwork@outlook.com>
1 parent f639f73 commit 3a7da96

4 files changed

Lines changed: 43 additions & 7 deletions

File tree

spring-cloud-alibaba-starters/spring-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySource.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,31 @@ public class NacosPropertySource extends MapPropertySource {
5555
*/
5656
private final boolean isRefreshable;
5757

58+
/**
59+
* File extension (suffix) of the config data (e.g., "properties", "yml", "json").
60+
*/
61+
private final String suffix;
62+
5863
NacosPropertySource(String group, String dataId, Map<String, Object> source,
59-
Date timestamp, boolean isRefreshable) {
64+
Date timestamp, boolean isRefreshable, String suffix) {
6065
super(String.join(NacosConfigProperties.COMMAS, dataId, group), source);
6166
this.group = group;
6267
this.dataId = dataId;
6368
this.timestamp = timestamp;
6469
this.isRefreshable = isRefreshable;
70+
this.suffix = suffix;
6571
}
6672

6773
public NacosPropertySource(List<PropertySource<?>> propertySources, String group,
6874
String dataId, Date timestamp, boolean isRefreshable) {
6975
this(group, dataId, getSourceMap(group, dataId, propertySources), timestamp,
70-
isRefreshable);
76+
isRefreshable, "properties");
77+
}
78+
79+
public NacosPropertySource(List<PropertySource<?>> propertySources, String group,
80+
String dataId, Date timestamp, boolean isRefreshable, String suffix) {
81+
this(group, dataId, getSourceMap(group, dataId, propertySources), timestamp,
82+
isRefreshable, suffix);
7183
}
7284

7385
private static Map<String, Object> getSourceMap(String group, String dataId,
@@ -129,4 +141,8 @@ public boolean isRefreshable() {
129141
return isRefreshable;
130142
}
131143

144+
public String getSuffix() {
145+
return suffix;
146+
}
147+
132148
}

spring-cloud-alibaba-starters/spring-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public NacosPropertySource build(String dataId, String group, String fileExtensi
7474
List<PropertySource<?>> propertySources = loadNacosData(dataId, group,
7575
fileExtension);
7676
NacosPropertySource nacosPropertySource = new NacosPropertySource(propertySources,
77-
group, dataId, new Date(), isRefreshable);
77+
group, dataId, new Date(), isRefreshable, fileExtension);
7878
NacosPropertySourceRepository.collectNacosPropertySource(nacosPropertySource);
7979
return nacosPropertySource;
8080
}

spring-cloud-alibaba-starters/spring-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public ConfigData doLoad(ConfigDataLoaderContext context,
8484

8585
NacosPropertySource propertySource = new NacosPropertySource(propertySources,
8686
config.getGroup(), config.getDataId(), new Date(),
87-
config.isRefreshEnabled());
87+
config.isRefreshEnabled(), config.getSuffix());
8888

8989
NacosPropertySourceRepository.collectNacosPropertySource(propertySource);
9090

spring-cloud-alibaba-starters/spring-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/refresh/NacosPropertySourceRefreshListener.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,32 @@ public void handle(NacosConfigRefreshEvent event) {
100100

101101
NacosPropertySourceBuilder nacosPropertySourceBuilder = new NacosPropertySourceBuilder(nacosConfigManager.getConfigService(), nacosConfigManager.getNacosConfigProperties()
102102
.getTimeout());
103-
String sourceName = String.join(NacosConfigProperties.COMMAS, event.dataId, event.group);
103+
104+
// Try both naming conventions: bootstrap path (dataId,group) and ConfigData path (group@dataId)
105+
String bootstrapName = String.join(NacosConfigProperties.COMMAS, event.dataId, event.group);
106+
String configDataName = event.group + "@" + event.dataId;
107+
104108
ConfigurableEnvironment environment = ((ConfigurableApplicationContext) applicationContext).getEnvironment();
105109
MutablePropertySources target = environment.getPropertySources();
106-
PropertySource<?> prevpropertySource = target.get(sourceName);
110+
111+
PropertySource<?> prevpropertySource = target.get(bootstrapName);
112+
String sourceName = bootstrapName;
113+
114+
// If not found with bootstrap naming, try ConfigData naming
115+
if (prevpropertySource == null) {
116+
prevpropertySource = target.get(configDataName);
117+
sourceName = configDataName;
118+
}
119+
107120
if (prevpropertySource instanceof NacosPropertySource) {
108-
NacosPropertySource newProperSource = nacosPropertySourceBuilder.build(event.getDataId(), event.getGroup(), "properties", ((NacosPropertySource) prevpropertySource).isRefreshable());
121+
NacosPropertySource prevNacosSource = (NacosPropertySource) prevpropertySource;
122+
// Use the actual suffix from the previous source instead of hardcoding "properties"
123+
String fileExtension = prevNacosSource.getSuffix();
124+
if (fileExtension == null || fileExtension.isEmpty()) {
125+
fileExtension = "properties";
126+
}
127+
NacosPropertySource newProperSource = nacosPropertySourceBuilder.build(
128+
event.getDataId(), event.getGroup(), fileExtension, prevNacosSource.isRefreshable());
109129
target.replace(sourceName, newProperSource);
110130
log.info("Replace Nacos Property Source : " + sourceName);
111131

0 commit comments

Comments
 (0)