From 51e861eecae022bc94f329afe252bc4cfbf5c6b5 Mon Sep 17 00:00:00 2001 From: 0032 Date: Fri, 22 Mar 2019 22:24:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Apollo=E4=BE=9D=E8=B5=96=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=85=8D=E7=BD=AE=E6=95=B0=E6=8D=AE=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E9=80=BB=E8=BE=91=EF=BC=8C=E6=94=AF=E6=8C=81=E4=B8=AA?= =?UTF-8?q?=E6=80=A7=E5=8C=96=E9=85=8D=E7=BD=AE=E4=B8=AD=E7=9A=84=E9=98=BF?= =?UTF-8?q?=E6=B3=A2=E7=BD=97=E7=9A=84=E9=85=8D=E7=BD=AE=E8=AF=BB=E5=8F=96?= =?UTF-8?q?,=E5=85=B6=E4=BB=96=E4=B8=AA=E6=80=A7=E5=8C=96=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=9C=AA=E5=A4=84=E7=90=86=20=E6=9B=B4=E6=96=B0starte?= =?UTF-8?q?r=E7=9A=84=E5=8D=95=E7=8B=AC=E7=89=88=E6=9C=AC2.7.7-release?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/spring-boot2-starter/pom.xml | 10 ++- .../support/util/SpringJ2CacheConfigUtil.java | 70 ++++++++++++------- 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/modules/spring-boot2-starter/pom.xml b/modules/spring-boot2-starter/pom.xml index 20e9b2a..70b2e83 100644 --- a/modules/spring-boot2-starter/pom.xml +++ b/modules/spring-boot2-starter/pom.xml @@ -9,7 +9,7 @@ ../../pom.xml 4.0.0 - + 2.7.7-release j2cache-spring-boot2-starter @@ -55,6 +55,13 @@ junit test + + + com.ctrip.framework.apollo + apollo-client + ${apollo.version} + + src @@ -74,5 +81,6 @@ 2.0.1.RELEASE + 1.1.0 \ No newline at end of file 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 8824ca2..68cbdc8 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 @@ -1,47 +1,69 @@ package net.oschina.j2cache.cache.support.util; +import com.ctrip.framework.apollo.spring.config.ConfigPropertySource; +import net.oschina.j2cache.J2CacheConfig; +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; -import net.oschina.j2cache.J2CacheConfig; - public class SpringJ2CacheConfigUtil { /** * 从spring环境变量中查找j2cache配置 */ - public final static J2CacheConfig initFromConfig(StandardEnvironment environment){ + public static final J2CacheConfig initFromConfig(StandardEnvironment environment) { J2CacheConfig config = new J2CacheConfig(); config.setSerialization(environment.getProperty("j2cache.serialization")); config.setBroadcast(environment.getProperty("j2cache.broadcast")); config.setL1CacheName(environment.getProperty("j2cache.L1.provider_class")); config.setL2CacheName(environment.getProperty("j2cache.L2.provider_class")); config.setSyncTtlToRedis(!"false".equalsIgnoreCase(environment.getProperty("j2cache.sync_ttl_to_redis"))); - config.setDefaultCacheNullObject("true".equalsIgnoreCase(environment.getProperty("j2cache.default_cache_null_object"))); + config.setDefaultCacheNullObject( + "true".equalsIgnoreCase(environment.getProperty("j2cache.default_cache_null_object"))); String l2_config_section = environment.getProperty("j2cache.L2.config_section"); if (l2_config_section == null || l2_config_section.trim().equals("")) l2_config_section = config.getL2CacheName(); final String l2_section = l2_config_section; - environment.getPropertySources().forEach(a -> { - if(a instanceof MapPropertySource) { - MapPropertySource c = (MapPropertySource) a; - c.getSource().forEach((k,v) -> { - String key = k; - 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)); - } - }); - } - }); + + environment.getPropertySources().forEach(a -> loadConfig(a, environment, config, l2_section)); + return config; } + + public static void loadConfig(PropertySource a, Environment environment, J2CacheConfig config, String l2_section) { + if (a instanceof CompositePropertySource) { + + ((CompositePropertySource) a) + .getPropertySources() + .forEach(b -> loadConfig(b, environment, config, l2_section)); + } else if (a instanceof MapPropertySource) { + MapPropertySource c = (MapPropertySource) a; + c.getSource().forEach((k, v) -> {}); + + } else if (a instanceof ConfigPropertySource) { + for (String propertyName : ((ConfigPropertySource) a).getPropertyNames()) { + loadConfigValue(environment, config, l2_section, propertyName); + } + } + } + + public static void loadConfigValue(Environment environment, J2CacheConfig config, String l2_section, String key) { + 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)); + } + } } -- Gitee