From 954c406731fdef129e545bc21260dd3ecfa0dac2 Mon Sep 17 00:00:00 2001 From: watilion Date: Tue, 9 Mar 2021 20:31:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E4=BD=BF=E7=94=A8spring-?= =?UTF-8?q?cloud-starter-alibaba-nacos-config=E9=9B=86=E6=88=90nacos?= =?UTF-8?q?=E6=97=B6=E6=97=A0=E6=B3=95=E5=8A=A0=E8=BD=BD=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support/util/SpringJ2CacheConfigUtil.java | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/modules/spring-boot2-starter/src/net/oschina/j2cache/cache/support/util/SpringJ2CacheConfigUtil.java b/modules/spring-boot2-starter/src/net/oschina/j2cache/cache/support/util/SpringJ2CacheConfigUtil.java index d554ecc..e3cf870 100644 --- a/modules/spring-boot2-starter/src/net/oschina/j2cache/cache/support/util/SpringJ2CacheConfigUtil.java +++ b/modules/spring-boot2-starter/src/net/oschina/j2cache/cache/support/util/SpringJ2CacheConfigUtil.java @@ -2,6 +2,7 @@ package net.oschina.j2cache.cache.support.util; import net.oschina.j2cache.J2CacheConfig; import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.StandardEnvironment; @@ -49,26 +50,31 @@ public class SpringJ2CacheConfigUtil { //配置在 nacos 中时,上面那段代码无法获取配置 if (config.getL1CacheProperties().isEmpty() || config.getL2CacheProperties().isEmpty() || config.getBroadcastProperties().isEmpty()) { environment.getPropertySources().forEach(a -> { - if (a instanceof CompositePropertySource) { - CompositePropertySource c = (CompositePropertySource) a; - String[] propertyNames = c.getPropertyNames(); - - for (String key : propertyNames) { - if (key.startsWith(config.getBroadcast() + ".")) { - config.getBroadcastProperties().setProperty(key.substring((config.getBroadcast() + ".").length()), - environment.getProperty(key)); - } - if (key.startsWith(config.getL1CacheName() + ".")) { - config.getL1CacheProperties().setProperty(key.substring((config.getL1CacheName() + ".").length()), - environment.getProperty(key)); - } - if (key.startsWith(l2_section + ".")) { - config.getL2CacheProperties().setProperty(key.substring((l2_section + ".").length()), - environment.getProperty(key)); - } - } - } - }); + String[] propertyNames = new String[0]; + if (a instanceof CompositePropertySource) { + CompositePropertySource c = (CompositePropertySource) a; + propertyNames = c.getPropertyNames(); + } else if (a instanceof EnumerablePropertySource){ + EnumerablePropertySource c = (EnumerablePropertySource) a; + propertyNames = c.getPropertyNames(); + } + if (propertyNames.length > 0){ + for (String key : propertyNames) { + if (key.startsWith(config.getBroadcast() + ".")) { + config.getBroadcastProperties().setProperty(key.substring((config.getBroadcast() + ".").length()), + environment.getProperty(key)); + } + if (key.startsWith(config.getL1CacheName() + ".")) { + config.getL1CacheProperties().setProperty(key.substring((config.getL1CacheName() + ".").length()), + environment.getProperty(key)); + } + if (key.startsWith(l2_section + ".")) { + config.getL2CacheProperties().setProperty(key.substring((l2_section + ".").length()), + environment.getProperty(key)); + } + } + } + }); } return config; } -- Gitee