From d5eb7b71cf0a45aa33ef76cedccf243b55f676b5 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Thu, 15 Dec 2022 10:40:10 +0800 Subject: [PATCH] refactor: remove get root application context method from halo plugin manager (#2949) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area core #### What this PR does / why we need it: HaloPluginManager 移除 getRootApplicationContext 方法 #### Which issue(s) this PR fixes: Fixes #2943 #### Special notes for your reviewer: /cc @halo-dev/sig-halo #### Does this PR introduce a user-facing change? ```release-note HaloPluginManager 移除 getRootApplicationContext 方法 ``` --- .../java/run/halo/app/plugin/BasePlugin.java | 5 +++-- .../run/halo/app/plugin/HaloPluginManager.java | 13 +++++-------- .../plugin/PluginApplicationInitializer.java | 17 +++++++++-------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/main/java/run/halo/app/plugin/BasePlugin.java b/src/main/java/run/halo/app/plugin/BasePlugin.java index 2c5ed0366..817aa436d 100644 --- a/src/main/java/run/halo/app/plugin/BasePlugin.java +++ b/src/main/java/run/halo/app/plugin/BasePlugin.java @@ -2,6 +2,7 @@ package run.halo.app.plugin; import lombok.extern.slf4j.Slf4j; import org.pf4j.Plugin; +import org.pf4j.PluginManager; import org.pf4j.PluginWrapper; /** @@ -18,7 +19,7 @@ public class BasePlugin extends Plugin { super(wrapper); } - private HaloPluginManager getPluginManager() { - return (HaloPluginManager) getWrapper().getPluginManager(); + private PluginManager getPluginManager() { + return getWrapper().getPluginManager(); } } diff --git a/src/main/java/run/halo/app/plugin/HaloPluginManager.java b/src/main/java/run/halo/app/plugin/HaloPluginManager.java index fe45abf45..97dfb673c 100644 --- a/src/main/java/run/halo/app/plugin/HaloPluginManager.java +++ b/src/main/java/run/halo/app/plugin/HaloPluginManager.java @@ -68,17 +68,13 @@ public class HaloPluginManager extends DefaultPluginManager return new SpringComponentsFinder(this); } - public ApplicationContext getRootApplicationContext() { - return this.rootApplicationContext; - } - @Override - public void setApplicationContext(@NonNull ApplicationContext rootApplicationContext) + public final void setApplicationContext(@NonNull ApplicationContext rootApplicationContext) throws BeansException { this.rootApplicationContext = rootApplicationContext; } - public PluginApplicationContext getPluginApplicationContext(String pluginId) { + final PluginApplicationContext getPluginApplicationContext(String pluginId) { return pluginApplicationInitializer.getPluginApplicationContext(pluginId); } @@ -88,8 +84,9 @@ public class HaloPluginManager extends DefaultPluginManager } @Override - public void afterPropertiesSet() { - this.pluginApplicationInitializer = new PluginApplicationInitializer(this); + public final void afterPropertiesSet() { + this.pluginApplicationInitializer = + new PluginApplicationInitializer(this, rootApplicationContext); this.requestMappingManager = rootApplicationContext.getBean(PluginRequestMappingManager.class); diff --git a/src/main/java/run/halo/app/plugin/PluginApplicationInitializer.java b/src/main/java/run/halo/app/plugin/PluginApplicationInitializer.java index 1e22b101d..21d32ab0f 100644 --- a/src/main/java/run/halo/app/plugin/PluginApplicationInitializer.java +++ b/src/main/java/run/halo/app/plugin/PluginApplicationInitializer.java @@ -26,17 +26,18 @@ public class PluginApplicationInitializer { private final ExtensionContextRegistry contextRegistry = ExtensionContextRegistry.getInstance(); private final SharedApplicationContextHolder sharedApplicationContextHolder; + private final ApplicationContext rootApplicationContext; - public PluginApplicationInitializer(HaloPluginManager springPluginManager) { - this.haloPluginManager = springPluginManager; - sharedApplicationContextHolder = springPluginManager.getRootApplicationContext() + public PluginApplicationInitializer(HaloPluginManager haloPluginManager, + ApplicationContext rootApplicationContext) { + Assert.notNull(haloPluginManager, "The haloPluginManager must not be null"); + Assert.notNull(rootApplicationContext, "The rootApplicationContext must not be null"); + this.haloPluginManager = haloPluginManager; + this.rootApplicationContext = rootApplicationContext; + sharedApplicationContextHolder = rootApplicationContext .getBean(SharedApplicationContextHolder.class); } - public ApplicationContext getRootApplicationContext() { - return this.haloPluginManager.getRootApplicationContext(); - } - private PluginApplicationContext createPluginApplicationContext(String pluginId) { PluginWrapper plugin = haloPluginManager.getPlugin(pluginId); ClassLoader pluginClassLoader = plugin.getPluginClassLoader(); @@ -111,7 +112,7 @@ public class PluginApplicationInitializer { private void populateSettingFetcher(String pluginName, DefaultListableBeanFactory listableBeanFactory) { ExtensionClient extensionClient = - getRootApplicationContext().getBean(ExtensionClient.class); + rootApplicationContext.getBean(ExtensionClient.class); SettingFetcher settingFetcher = new SettingFetcher(pluginName, extensionClient); listableBeanFactory.registerSingleton("settingFetcher", settingFetcher); }