Skip to content

Commit 16ddf58

Browse files
authored
[2021.x]Separate nacos ConfigService initialization from normal properties bean. (#3787)
1 parent 4079d32 commit 16ddf58

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ private String resolveNamespace() {
593593
}
594594
}
595595

596-
private void enrichNacosConfigProperties(Properties nacosConfigProperties) {
596+
protected void enrichNacosConfigProperties(Properties nacosConfigProperties) {
597597
if (environment == null) {
598598
return;
599599
}
@@ -603,7 +603,7 @@ private void enrichNacosConfigProperties(Properties nacosConfigProperties) {
603603
String.valueOf(v)));
604604
}
605605

606-
private String resolveKey(String key) {
606+
protected String resolveKey(String key) {
607607
Matcher matcher = PATTERN.matcher(key);
608608
StringBuffer sb = new StringBuffer();
609609
while (matcher.find()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.cloud.nacos.configdata;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
import java.util.Properties;
22+
23+
import com.alibaba.cloud.nacos.NacosConfigProperties;
24+
25+
/**
26+
* Used for initialization of Nacos ConfigService.
27+
*/
28+
public class NacosConfigDataLoadProperties extends NacosConfigProperties {
29+
private Map<String, String> config = new HashMap<>();
30+
31+
@Override
32+
protected void enrichNacosConfigProperties(Properties nacosConfigProperties) {
33+
config.forEach((k, v) -> nacosConfigProperties.putIfAbsent(resolveKey(k),
34+
String.valueOf(v)));
35+
}
36+
37+
Map<String, String> getConfig() {
38+
return config;
39+
}
40+
41+
void setConfig(Map<String, String> config) {
42+
this.config = config;
43+
}
44+
}

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

+9-13
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,12 @@ public class NacosConfigDataLocationResolver
5959
* Prefix for Config Server imports.
6060
*/
6161
public static final String PREFIX = "nacos:";
62-
63-
private final Log log;
64-
65-
// support params
66-
6762
private static final String GROUP = "group";
6863

64+
// support params
6965
private static final String REFRESH_ENABLED = "refreshEnabled";
70-
7166
private static final String PREFERENCE = "preference";
67+
private final Log log;
7268

7369
public NacosConfigDataLocationResolver(DeferredLogFactory logFactory) {
7470
this.log = logFactory.getLog(getClass());
@@ -85,22 +81,22 @@ protected NacosConfigProperties loadProperties(
8581
BindHandler bindHandler = getBindHandler(context);
8682

8783
NacosConfigProperties nacosConfigProperties;
88-
if (context.getBootstrapContext().isRegistered(NacosConfigProperties.class)) {
84+
if (context.getBootstrapContext().isRegistered(NacosConfigDataLoadProperties.class)) {
8985
nacosConfigProperties = context.getBootstrapContext()
90-
.get(NacosConfigProperties.class);
86+
.get(NacosConfigDataLoadProperties.class);
9187
}
9288
else {
9389
nacosConfigProperties = binder
94-
.bind("spring.cloud.nacos", Bindable.of(NacosConfigProperties.class),
90+
.bind("spring.cloud.nacos", Bindable.of(NacosConfigDataLoadProperties.class),
9591
bindHandler)
9692
.map(properties -> binder
97-
.bind(NacosConfigProperties.PREFIX,
93+
.bind(NacosConfigDataLoadProperties.PREFIX,
9894
Bindable.ofInstance(properties), bindHandler)
9995
.orElse(properties))
10096
.orElseGet(() -> binder
101-
.bind(NacosConfigProperties.PREFIX,
102-
Bindable.of(NacosConfigProperties.class), bindHandler)
103-
.orElseGet(NacosConfigProperties::new));
97+
.bind(NacosConfigDataLoadProperties.PREFIX,
98+
Bindable.of(NacosConfigDataLoadProperties.class), bindHandler)
99+
.orElseGet(NacosConfigDataLoadProperties::new));
104100
}
105101

106102
return nacosConfigProperties;

0 commit comments

Comments
 (0)