diff --git a/pom.xml b/pom.xml index 5df6ef751672a1a156a381adfc09d971f543fd07..23ca7cda17f330ee59d09750c74298a2bcdfb316 100644 --- a/pom.xml +++ b/pom.xml @@ -29,8 +29,49 @@ 1.8 UTF-8 3.8.1 + + 3.1.0 + 3.1.0 + 3.1.0 + 1.6 + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + https://gitee.com/starblues/springboot-plugin-framework-parent + scm:https://gitee.com/starblues/springboot-plugin-framework-parent.git + scm:https://gitee.com/starblues/springboot-plugin-framework-parent.git + 1.0 + + + + + sonatype-nexus-snapshots + oss Snapshots Repository + https://oss.sonatype.org/content/repositories/snapshots + + + sonatype-nexus-staging + oss Staging Repository + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + + + StarBlues + starblues@foxmail.com + https://gitee.com/starblues/ + + + @@ -42,8 +83,106 @@ ${java.version} + + + org.apache.maven.plugins + maven-assembly-plugin + ${maven-assembly-plugin.version} + + + jar-with-dependencies + + ${project.artifactId}-${project.version} + false + false + + + true + true + + + + + + make-assembly + package + + single + + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven-source-plugin.version} + + + package + + jar-no-fork + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + ${plugin.skip} + + + + package + + jar + + + + + + + org.apache.maven.plugins + maven-gpg-plugin + ${maven-gpg-plugin.version} + + ${plugin.skip} + + + + sign-artifacts + verify + + sign + + + + + + + dev + + true + + + true + + + + + release + + false + + + + + \ No newline at end of file diff --git a/spring-plugin-framework-common/src/main/java/com/gitee/starblues/utils/ObjectUtils.java b/spring-plugin-framework-common/src/main/java/com/gitee/starblues/utils/ObjectUtils.java index 4ec35ae1bbf79e316d81132069fa70e8e4562ae6..ba1baca9e73aeb386015fea1c5db717b40d0eb80 100644 --- a/spring-plugin-framework-common/src/main/java/com/gitee/starblues/utils/ObjectUtils.java +++ b/spring-plugin-framework-common/src/main/java/com/gitee/starblues/utils/ObjectUtils.java @@ -423,6 +423,11 @@ public class ObjectUtils { return count; } + public static String changePackageToMatch(String packageName){ + Assert.isNotEmpty(packageName, "参数 packageName 不能为空"); + return packageName.replace(".", "/") + "/**"; + } + private static boolean containsText(CharSequence str) { int strLen = str.length(); for (int i = 0; i < strLen; i++) { diff --git a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java index ea0bae35e67f405b9ce38ab00208b56bf10e89b6..c159c6e9b470648b34f8885e72c18d9f2c539328 100644 --- a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java +++ b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java @@ -73,8 +73,8 @@ class ConfigurePluginEnvironment { env.put(SPRING_JMX_UNIQUE_NAMES, true); env.put(SPRING_ADMIN_JMX_NAME, SPRING_ADMIN_JMX_VALUE + pluginId); env.put(REGISTER_SHUTDOWN_HOOK_PROPERTY, false); - env.put(MBEAN_DOMAIN_PROPERTY_NAME, null); - environment.getPropertySources().addLast(new MapPropertySource(PLUGIN_PROPERTY_NAME, env)); + env.put(MBEAN_DOMAIN_PROPERTY_NAME, pluginId); + environment.getPropertySources().addFirst(new MapPropertySource(PLUGIN_PROPERTY_NAME, env)); if(processorContext.runMode() == ProcessorContext.RunMode.ONESELF){ ConfigureMainPluginEnvironment configureMainPluginEnvironment = diff --git a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/EmptyMainApplicationContext.java b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/EmptyMainApplicationContext.java index 7b677469853cd105b387772ae95b1a7ed59e9497..53a55368e9971b990a1ed3c039d4ede5e69f721f 100644 --- a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/EmptyMainApplicationContext.java +++ b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/EmptyMainApplicationContext.java @@ -24,6 +24,7 @@ import org.springframework.beans.factory.ObjectProvider; import org.springframework.core.ResolvableType; import java.lang.annotation.Annotation; +import java.util.Collections; import java.util.Map; import java.util.Set; @@ -45,4 +46,9 @@ public class EmptyMainApplicationContext implements MainApplicationContext { public void close() throws Exception { } + + @Override + public Map> getConfigurableEnvironment() { + return Collections.emptyMap(); + } } diff --git a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/FrameDefineBeanProcessor.java b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/FrameDefineBeanProcessor.java index b4c6549182214e2c075d579466a8026f24932587..56fcb6af01916a9de51eaf1c8c57d56230c3229a 100644 --- a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/FrameDefineBeanProcessor.java +++ b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/FrameDefineBeanProcessor.java @@ -17,6 +17,9 @@ package com.gitee.starblues.bootstrap.processor; +import com.gitee.starblues.bootstrap.realize.DefaultMainEnvironmentProvider; +import com.gitee.starblues.bootstrap.realize.EmptyMainEnvironmentProvider; +import com.gitee.starblues.bootstrap.realize.MainEnvironmentProvider; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.integration.AutoIntegrationConfiguration; import com.gitee.starblues.integration.ExtendPointConfiguration; @@ -36,11 +39,17 @@ public class FrameDefineBeanProcessor implements SpringPluginProcessor { InsidePluginDescriptor pluginDescriptor = context.getPluginDescriptor(); ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory(); beanFactory.registerSingleton("pluginDescriptor", pluginDescriptor.toPluginDescriptor()); + beanFactory.registerSingleton("mainApplicationContext", context.getMainApplicationContext()); + MainEnvironmentProvider mainEnvironmentProvider = null; if(context.runMode() == ProcessorContext.RunMode.ONESELF){ beanFactory.registerSingleton("integrationConfiguration", new AutoIntegrationConfiguration()); applicationContext.registerBean(ExtendPointConfiguration.class); + mainEnvironmentProvider = new EmptyMainEnvironmentProvider(); + } else { + mainEnvironmentProvider = new DefaultMainEnvironmentProvider(context.getMainApplicationContext()); } + beanFactory.registerSingleton("mainEnvironmentProvider", mainEnvironmentProvider); } @Override diff --git a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/DefaultMainEnvironmentProvider.java b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/DefaultMainEnvironmentProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..8487d3664dd6c607272685cccc47dd56471c2aaf --- /dev/null +++ b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/DefaultMainEnvironmentProvider.java @@ -0,0 +1,123 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.starblues.bootstrap.realize; + +import com.gitee.starblues.loader.utils.ObjectUtils; +import com.gitee.starblues.spring.MainApplicationContext; + +import java.util.Map; +import java.util.function.Function; + +/** + * 主程序配置信息提供者默认实现 + * + * @author starBlues + * @version 3.0.0 + */ +public class DefaultMainEnvironmentProvider implements MainEnvironmentProvider{ + + private final MainApplicationContext mainApplicationContext; + + public DefaultMainEnvironmentProvider(MainApplicationContext mainApplicationContext) { + this.mainApplicationContext = mainApplicationContext; + } + + @Override + public Object getValue(String name) { + Map> configurableEnvironment = mainApplicationContext.getConfigurableEnvironment(); + if(ObjectUtils.isEmpty(configurableEnvironment)){ + return null; + } + for (Map.Entry> entry : configurableEnvironment.entrySet()) { + Map value = entry.getValue(); + Object o = value.get(name); + if(o != null){ + return o; + } + } + return null; + } + + @Override + public String getString(String name) { + return getValue(name, String::valueOf); + } + + @Override + public Integer getInteger(String name) { + return getValue(name, value -> { + if(value instanceof Integer){ + return (Integer) value; + } + return Integer.parseInt(String.valueOf(value)); + }); + } + + @Override + public Long getLong(String name) { + return getValue(name, value -> { + if(value instanceof Long){ + return (Long) value; + } + return Long.parseLong(String.valueOf(value)); + }); + } + + @Override + public Double getDouble(String name) { + return getValue(name, value -> { + if(value instanceof Double){ + return (Double) value; + } + return Double.parseDouble(String.valueOf(value)); + }); + } + + @Override + public Float getFloat(String name) { + return getValue(name, value -> { + if(value instanceof Float){ + return (Float) value; + } + return Float.parseFloat(String.valueOf(value)); + }); + } + + @Override + public Boolean getBoolean(String name) { + return getValue(name, value -> { + if(value instanceof Boolean){ + return (Boolean) value; + } + return Boolean.parseBoolean(String.valueOf(value)); + }); + } + + @Override + public Map> getAll() { + return mainApplicationContext.getConfigurableEnvironment(); + } + + private T getValue(String name, Function function){ + Object value = getValue(name); + if(value == null){ + return null; + } + return function.apply(value); + } + +} diff --git a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/EmptyMainEnvironmentProvider.java b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/EmptyMainEnvironmentProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..8180226fff2cf9e92bb7b7ba1e5fd56c0ca417c0 --- /dev/null +++ b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/EmptyMainEnvironmentProvider.java @@ -0,0 +1,68 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.starblues.bootstrap.realize; + +import java.util.Collections; +import java.util.Map; + +/** + * 主程序配置信息提供者空值实现 + * + * @author starBlues + * @version 3.0.0 + */ +public class EmptyMainEnvironmentProvider implements MainEnvironmentProvider{ + @Override + public Object getValue(String name) { + return null; + } + + @Override + public String getString(String name) { + return null; + } + + @Override + public Integer getInteger(String name) { + return null; + } + + @Override + public Long getLong(String name) { + return null; + } + + @Override + public Double getDouble(String name) { + return null; + } + + @Override + public Float getFloat(String name) { + return null; + } + + @Override + public Boolean getBoolean(String name) { + return null; + } + + @Override + public Map> getAll() { + return Collections.emptyMap(); + } +} diff --git a/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/MainEnvironmentProvider.java b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/MainEnvironmentProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..ea68a164bec6f04e3b4c5e32c73b51c1f778c8b6 --- /dev/null +++ b/springboot-plugin-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/MainEnvironmentProvider.java @@ -0,0 +1,84 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.starblues.bootstrap.realize; + +import java.util.Map; + +/** + * 主程序配置信息提供者 + * + * @author starBlues + * @version 3.0.0 + */ +public interface MainEnvironmentProvider { + + /** + * 根据名称获取配置值 + * @param name 配置名称 + * @return 配置值 + */ + Object getValue(String name); + + /** + * 根据名称获取 String 类型配置值 + * @param name 配置名称 + * @return 配置值 + */ + String getString(String name); + + /** + * 根据名称获取 Integer 类型配置值 + * @param name 配置名称 + * @return 配置值 + */ + Integer getInteger(String name); + + /** + * 根据名称获取 Long 类型配置值 + * @param name 配置名称 + * @return 配置值 + */ + Long getLong(String name); + + /** + * 根据名称获取 Double 类型配置值 + * @param name 配置名称 + * @return 配置值 + */ + Double getDouble(String name); + + /** + * 根据名称获取 Float 类型配置值 + * @param name 配置名称 + * @return 配置值 + */ + Float getFloat(String name); + + /** + * 根据名称获取 Boolean 类型配置值 + * @param name 配置名称 + * @return 配置值 + */ + Boolean getBoolean(String name); + + /** + * 获取所有配置集合 + * @return Map + */ + Map> getAll(); + +} diff --git a/springboot-plugin-framework/pom.xml b/springboot-plugin-framework/pom.xml index 4c832fe19391cfc09e2222d47d3aa0ca8554103e..a5ad6f0d63bc72029bd9d644406debc44b6808aa 100644 --- a/springboot-plugin-framework/pom.xml +++ b/springboot-plugin-framework/pom.xml @@ -16,55 +16,14 @@ spring boot 插件式开发集成包 - - - - The Apache Software License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - - - - https://gitee.com/starblues/springboot-plugin-framework-parent - scm:https://gitee.com/starblues/springboot-plugin-framework-parent.git - scm:https://gitee.com/starblues/springboot-plugin-framework-parent.git - 1.0 - - - - - sonatype-nexus-snapshots - oss Snapshots Repository - https://oss.sonatype.org/content/repositories/snapshots - - - sonatype-nexus-staging - oss Staging Repository - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - - - - StarBlues - starblues@foxmail.com - https://gitee.com/starblues/ - - - - 3.1.0 - 3.1.0 - 3.1.0 - 1.6 - 2.10.1 1.7.7 2.11.0 5.3.12 2.5.6 + 3.0.12.RELEASE 2.10.5 1.5.2 4.0.1 @@ -116,7 +75,7 @@ org.thymeleaf thymeleaf-spring5 - 3.0.12.RELEASE + ${thymeleaf-spring5.version} provided true @@ -156,7 +115,6 @@ - @@ -178,105 +136,7 @@ - - - org.apache.maven.plugins - maven-assembly-plugin - ${maven-assembly-plugin.version} - - - jar-with-dependencies - - ${project.artifactId}-${project.version} - false - false - - - true - true - - - - - - make-assembly - package - - single - - - - - - - org.apache.maven.plugins - maven-source-plugin - ${maven-source-plugin.version} - - - package - - jar-no-fork - - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - ${maven-javadoc-plugin.version} - - ${plugin.skip} - - - - package - - jar - - - - - - - org.apache.maven.plugins - maven-gpg-plugin - ${maven-gpg-plugin.version} - - ${plugin.skip} - - - - sign-artifacts - verify - - sign - - - - - - - dev - - true - - - true - - - - - release - - false - - - - - \ No newline at end of file diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/DefaultPluginManager.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/DefaultPluginManager.java index 580378ece11d5cb444d5c205445ade8891df2e4f..50b3e555289776fdd2ffbb45608fa352a85e0b6e 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/DefaultPluginManager.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/DefaultPluginManager.java @@ -131,6 +131,7 @@ public class DefaultPluginManager implements PluginManager{ warn.append(i + 1).append(". ").append(pluginRootDirs.get(i)).append("\n"); } warn.append("请检查路径是否合适.\n"); + warn.append("请检查配置[plugin.runMode]是否合适.\n"); if(provider.getRuntimeMode() == RuntimeMode.DEV){ warn.append("请检查插件包是否编译.\n"); } else { diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java index d825433d2acdcd2c2bad65cc0a0936d8d945c1cb..432fcf62d4c2491517c7f42f5ffe67eea0441b3d 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java @@ -60,9 +60,7 @@ public class PluginLauncherManager extends DefaultPluginManager{ GenericApplicationContext applicationContext, IntegrationConfiguration configuration) { super(realizeProvider, configuration); - this.mainApplicationContext = new MainApplicationContextProxy( - applicationContext.getBeanFactory(), - applicationContext); + this.mainApplicationContext = new MainApplicationContextProxy(applicationContext, applicationContext); this.mainGenericApplicationContext = applicationContext; this.configuration = configuration; this.invokeSupperCache = new DefaultInvokeSupperCache(); diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/launcher/plugin/BasicMainResourcePatternDefiner.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/launcher/plugin/BasicMainResourcePatternDefiner.java index c3a2eb5a115aa64fd90db455d2630b211af71a95..461a5ca27482da906c19ab52eab3a29d0c6a3277 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/launcher/plugin/BasicMainResourcePatternDefiner.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/launcher/plugin/BasicMainResourcePatternDefiner.java @@ -17,6 +17,8 @@ package com.gitee.starblues.core.launcher.plugin; import com.gitee.starblues.core.classloader.MainResourcePatternDefiner; +import com.gitee.starblues.utils.Assert; +import com.gitee.starblues.utils.ObjectUtils; import java.util.HashSet; import java.util.Set; @@ -31,7 +33,7 @@ public class BasicMainResourcePatternDefiner implements MainResourcePatternDefin private final String mainPackageName; public BasicMainResourcePatternDefiner(String mainPackageName) { - this.mainPackageName = mainPackageName; + this.mainPackageName = ObjectUtils.changePackageToMatch(mainPackageName); } @Override diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginMainResourcePatternDefiner.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginMainResourcePatternDefiner.java index e998516f6d57f88e78231e54748f3cfa0a3f42c3..8772b542c3e8cd3d90184f6b84d07c1d4508e9f3 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginMainResourcePatternDefiner.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginMainResourcePatternDefiner.java @@ -19,6 +19,7 @@ package com.gitee.starblues.core.launcher.plugin; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.core.launcher.JavaMainResourcePatternDefiner; import com.gitee.starblues.spring.MainApplicationContext; +import com.gitee.starblues.utils.Assert; import com.gitee.starblues.utils.ObjectUtils; import com.gitee.starblues.utils.SpringBeanCustomUtils; @@ -53,7 +54,7 @@ public class PluginMainResourcePatternDefiner extends JavaMainResourcePatternDef if(!ObjectUtils.isEmpty(includePatterns)){ includeResourcePatterns.addAll(includePatterns); } else { - includeResourcePatterns.add(mainPackage); + includeResourcePatterns.add(ObjectUtils.changePackageToMatch(mainPackage)); } includeResourcePatterns.add(FRAMEWORK); addWebIncludeResourcePatterns(includeResourcePatterns); @@ -151,4 +152,6 @@ public class PluginMainResourcePatternDefiner extends JavaMainResourcePatternDef } } + + } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginMethodRunner.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginMethodRunner.java index 9515cdfdcb87353c71a2baac2f88796e637c3dc8..c5d12d4add3ba48709865563d5d1712f5b81e9eb 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginMethodRunner.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginMethodRunner.java @@ -79,8 +79,14 @@ public class PluginMethodRunner extends MethodRunner { } catch (Exception e){ String error = "Invoke failure: " + ReflectionUtils.methodToString(runClass, runMethodName, runMethod.getParameterTypes()) - + ". " + e.getMessage(); - logger.error(error, e); + + ". "; + String message = e.getMessage(); + if(message != null){ + error = error + message; + logger.error(error, e); + } else { + logger.error(error); + } throw new Exception(error); } } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/MainApplicationContext.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/MainApplicationContext.java index 9e4f1a4a0ebfb3ff597dfbfc36d6dc950ebfe60d..070e04a6b4f0d567069f32925f772b48cad0eb1e 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/MainApplicationContext.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/MainApplicationContext.java @@ -16,6 +16,8 @@ package com.gitee.starblues.spring; +import java.util.Map; + /** * 主程序 ApplicationContext 接口 * @author starBlues @@ -23,4 +25,11 @@ package com.gitee.starblues.spring; */ public interface MainApplicationContext extends ApplicationContext { + /** + * 得到主程序所有配置的 env + * + * @return 主程序配置的 env 集合 + */ + Map> getConfigurableEnvironment(); + } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/MainApplicationContextProxy.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/MainApplicationContextProxy.java index 11dfb1819200c558bdd94df79c1ca2437ab6a97b..a430ae8a831491cd595ffc4b39d603bfee909f97 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/MainApplicationContextProxy.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/spring/MainApplicationContextProxy.java @@ -16,6 +16,17 @@ package com.gitee.starblues.spring; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; + /** * 主程序 ApplicationContext 的实现 * @author starBlues @@ -23,11 +34,37 @@ package com.gitee.starblues.spring; */ public class MainApplicationContextProxy extends ApplicationContextProxy implements MainApplicationContext{ - public MainApplicationContextProxy(Object targetBeanFactory) { - super(targetBeanFactory); + private final GenericApplicationContext applicationContext; + + public MainApplicationContextProxy(GenericApplicationContext applicationContext) { + super(applicationContext.getBeanFactory()); + this.applicationContext = applicationContext; + } + + public MainApplicationContextProxy(GenericApplicationContext applicationContext, AutoCloseable autoCloseable) { + super(applicationContext.getBeanFactory(), autoCloseable); + this.applicationContext = applicationContext; } - public MainApplicationContextProxy(Object targetBeanFactory, AutoCloseable autoCloseable) { - super(targetBeanFactory, autoCloseable); + @Override + public Map> getConfigurableEnvironment() { + ConfigurableEnvironment environment = applicationContext.getEnvironment(); + MutablePropertySources propertySources = environment.getPropertySources(); + Map> environmentMap = new LinkedHashMap<>(propertySources.size()); + for (PropertySource propertySource : propertySources) { + if (!(propertySource instanceof EnumerablePropertySource)) { + continue; + } + EnumerablePropertySource enumerablePropertySource = (EnumerablePropertySource) propertySource; + String[] propertyNames = enumerablePropertySource.getPropertyNames(); + Map values = new HashMap<>(propertyNames.length); + for (String propertyName : propertyNames) { + values.put(propertyName, enumerablePropertySource.getProperty(propertyName)); + } + if (!values.isEmpty()) { + environmentMap.put(propertySource.getName(), values); + } + } + return environmentMap; } }