Custom plug-in factory
There are two ways to customize the plug-in factory:
Configure the plug-in factory through the
@PluginFactory
annotation. The annotation parameters are explained as follows:Sample code:
@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 { }
Instantiation via factory configuration object
Create factory configuration object:
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());
Create and initialize the plug-in factory instance object:
IPluginFactory _factory = new DefaultPluginFactory(); _factory.init(_conf);
Customize the event listening method of the plug-in factory:
The event processing method of the custom plug-in factory is different from the default plug-in factory. The plug-in life cycle event monitoring must be completed by implementing the IPluginEventListener interface. The IPluginEventListener interface event method and description are as follows:
Sample code:
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()); } }