自訂插件工廠
自訂外掛程式工廠有兩種方式:
透過
@PluginFactory
註解配置外掛程式工廠,註解參數說明如下:範例程式碼:
@PluginFactory(pluginHome = "${root}/plugins") public class DemoPluginFactory extends DefaultPluginFactory { } // 或者 @PluginFactory(pluginHome = "${root}/plugins", autoscanPackages = {"com.company", "cn.company"}, automatic = true, includedClassPath = false, listenerClass = DemoPluginEventListener.class) public class DemoPluginFactory extends DefaultPluginFactory { }
#透過工廠配置物件實例化
建立工廠配置物件:
DefaultPluginConfig _conf = new DefaultPluginConfig(); _conf.setPluginHome(new File(RuntimeUtils.replaceEnvVariable("${root}/plugins"))); _conf.setAutomatic(true); _conf.setAutoscanPackages(Arrays.asList("com.company", "cn.company")); _conf.setIncludedClassPath(false); _conf.setPluginEventListener(new DefaultPluginEventListener());
建立並初始化外掛程式工廠實例物件:
IPluginFactory _factory = new DefaultPluginFactory(); _factory.init(_conf);
自訂外掛程式工廠的事件監聽方法:
自訂外掛程式工廠的事件處理方式與預設外掛程式工廠不同,須透過實作IPluginEventListener介面完成插件生命週期事件監聽,IPluginEventListener介面事件方法及說明如下:
#範例程式碼:
public class DemoPluginEventListener implements IPluginEventListener { public void onInited(IPluginContext context, IPlugin plugin) { System.out.println("onInited: " + context.getPluginMeta().getName()); } public void onStarted(IPluginContext context, IPlugin plugin) { System.out.println("onStarted: " + context.getPluginMeta().getName()); } public void onShutdown(IPluginContext context, IPlugin plugin) { System.out.println("onShutdown: " + context.getPluginMeta().getName()); } public void onDestroy(IPluginContext context, IPlugin plugin) { System.out.println("onDestroy: " + context.getPluginMeta().getName()); } }