From fa0b5a8664bd418faf2da1533d29a813abc4e5ee Mon Sep 17 00:00:00 2001 From: flystar32 Date: Thu, 7 Mar 2019 20:58:07 +0800 Subject: [PATCH] Fixes #404 --- .../alibaba/nacos/NacosConfigProperties.java | 18 +----------------- .../client/NacosPropertySourceLocator.java | 18 ++++++++---------- .../alibaba/nacos/NacosConfigurationTests.java | 6 ------ 3 files changed, 9 insertions(+), 33 deletions(-) diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java index 5d13c01c5..666051459 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java @@ -122,8 +122,6 @@ public class NacosConfigProperties { private String name; - private String[] activeProfiles; - /** * the dataids for configurable multiple shared configurations , multiple separated by * commas . @@ -145,11 +143,6 @@ public class NacosConfigProperties { @Autowired private Environment environment; - @PostConstruct - public void init() { - this.activeProfiles = environment.getActiveProfiles(); - } - // todo sts support public boolean isEnabled() { @@ -260,10 +253,6 @@ public class NacosConfigProperties { return name; } - public String[] getActiveProfiles() { - return activeProfiles; - } - public String getSharedDataids() { return sharedDataids; } @@ -292,10 +281,6 @@ public class NacosConfigProperties { this.name = name; } - public void setActiveProfiles(String[] activeProfiles) { - this.activeProfiles = activeProfiles; - } - public static class Config { /** * the data id of extended configuration @@ -344,8 +329,7 @@ public class NacosConfigProperties { + endpoint + '\'' + ", namespace='" + namespace + '\'' + ", accessKey='" + accessKey + '\'' + ", secretKey='" + secretKey + '\'' + ", contextPath='" + contextPath + '\'' + ", clusterName='" + clusterName - + '\'' + ", name='" + name + '\'' + ", activeProfiles=" - + Arrays.toString(activeProfiles) + ", sharedDataids='" + sharedDataids + + '\'' + ", name='" + name + '\'' + ", sharedDataids='" + sharedDataids + '\'' + ", refreshableDataids='" + refreshableDataids + '\'' + ", extConfig=" + extConfig + '}'; } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java index 4c5606583..306066eb6 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java @@ -71,7 +71,6 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { timeout); String name = nacosConfigProperties.getName(); - String nacosGroup = nacosConfigProperties.getGroup(); String dataIdPrefix = nacosConfigProperties.getPrefix(); if (StringUtils.isEmpty(dataIdPrefix)) { dataIdPrefix = name; @@ -81,17 +80,12 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { dataIdPrefix = env.getProperty("spring.application.name"); } - List profiles = Arrays.asList(env.getActiveProfiles()); - nacosConfigProperties.setActiveProfiles(profiles.toArray(new String[0])); - - String fileExtension = nacosConfigProperties.getFileExtension(); - CompositePropertySource composite = new CompositePropertySource( NACOS_PROPERTY_SOURCE_NAME); loadSharedConfiguration(composite); loadExtConfiguration(composite); - loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension); + loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env); return composite; } @@ -152,11 +146,15 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { } private void loadApplicationConfiguration( - CompositePropertySource compositePropertySource, String nacosGroup, - String dataIdPrefix, String fileExtension) { + CompositePropertySource compositePropertySource, String dataIdPrefix, + NacosConfigProperties properties, Environment environment) { + + String fileExtension = properties.getFileExtension(); + String nacosGroup = properties.getGroup(); + loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true); - for (String profile : nacosConfigProperties.getActiveProfiles()) { + for (String profile : environment.getActiveProfiles()) { String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, fileExtension, true); diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigurationTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigurationTests.java index 34f275c2d..fc2a3b124 100644 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigurationTests.java +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigurationTests.java @@ -163,7 +163,6 @@ public class NacosConfigurationTests { checkoutNacosConfigFileExtension(); checkoutNacosConfigTimeout(); checkoutNacosConfigEncode(); - checkoutNacosConfigProfiles(); checkoutEndpoint(); checkoutDataLoad(); @@ -239,11 +238,6 @@ public class NacosConfigurationTests { Assert.assertEquals(environment.getProperty("user.age"), "12"); } - private void checkoutNacosConfigProfiles() { - assertEquals("NacosConfigProperties' profiles is wrong", - new String[] { "dev", "test" }, properties.getActiveProfiles()); - } - private void checkoutEndpoint() throws Exception { NacosConfigEndpoint nacosConfigEndpoint = new NacosConfigEndpoint(properties, refreshHistory);