首页  

pf4j 实例     所属分类 pf4j 浏览量 621
api  
定义插件接口 Greeting.java
import org.pf4j.ExtensionPoint;
public interface Greeting extends ExtensionPoint {
    String getGreeting();
}


core 插件实现 ,打包成jar ,放到指定的插件目录 ,由宿主程序加载运行 plugin1 插件1实现 WelcomeGreeting.java import org.pf4j.Extension; import dyyx.pf4jdemo.api.Greeting; @Extension public class WelcomeGreeting implements Greeting{ public String getGreeting() { return "Welcome"; } } WelcomePlugin.java import org.pf4j.Plugin; import org.pf4j.PluginWrapper; public class WelcomePlugin extends Plugin{ public WelcomePlugin(PluginWrapper wrapper) { super(wrapper); } @Override public void start() { System.out.println("WelcomePlugin.start()"); } @Override public void stop() { System.out.println("WelcomePlugin.stop()"); } @Override public void delete() { System.out.println("WelcomePlugin.delete()"); } } 打包插件 mvn clean package maven-jar-plugin 指定 MANIFEST 文件 Maven 打包 定制 manifest 把 生成的jar包放到宿主应用指定的插件目录下
app 宿主应用 ,从指定目录加载插件并运行 import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import org.pf4j.DefaultPluginManager; import org.pf4j.PluginManager; import org.pf4j.PluginWrapper; import dyyx.pf4jdemo.api.Greeting; public class PluginDemo{ public static void main(String[] args) throws Exception{ System.out.println("hello,pf4j"); // create the plugin manager // or "new ZipPluginManager() / new DefaultPluginManager()" // 不要带 file:// 前缀 String pluginRootDir = "/PATH/pf4jdemo/app/plugins"; Path pluginRoot = Paths.get(pluginRootDir); System.out.println("pluginRoot="+pluginRoot); // PluginManager pluginManager = new JarPluginManager(pluginRoot); PluginManager pluginManager = new DefaultPluginManager(pluginRoot); // start and load all plugins of application pluginManager.loadPlugins(); pluginManager.startPlugins(); List pluginWrappers = pluginManager.getPlugins(); for(PluginWrapper item:pluginWrappers) { System.out.println("PluginWrapper="+item); } System.out.println(); List resolvedPlugins = pluginManager.getResolvedPlugins(); for(PluginWrapper item:resolvedPlugins) { System.out.println("resolvedPlugins.PluginWrapper="+item); } // retrieve all extensions for "Greeting" extension point List greetings = pluginManager.getExtensions(Greeting.class); System.out.println("greetings plugin num "+greetings.size()); for (Greeting greeting : greetings) { System.out.println(greeting + "#" + greeting.getGreeting()); } // stop and unload all plugins pluginManager.stopPlugins(); pluginManager.unloadPlugins(); // Unknown pluginId welcome-plugin // pluginManager.deletePlugin("welcome-plugin"); Thread.sleep(3000); } }
pluginRoot=/PATH/pf4jdemo/app/plugins SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. WelcomePlugin.start() Welcome2Plugin.start() PluginWrapper=PluginWrapper [descriptor=PluginDescriptor [pluginId=welcome-plugin, pluginClass=dyyx.pf4jdemo.core.plugin1.WelcomePlugin, version=1.0.0, provider=dugang, dependencies=[], description=, requires=*, license=null], pluginPath=/PATH/pf4jdemo/app/plugins/pf4jdemo-plugin1-1.0.0.jar] PluginWrapper=PluginWrapper [descriptor=PluginDescriptor [pluginId=welcome-plugin2, pluginClass=dyyx.pf4jdemo.core.plugin2.Welcome2Plugin, version=1.0.0, provider=dugang, dependencies=[], description=, requires=*, license=null], pluginPath=/PATH/pf4jdemo/app/plugins/pf4jdemo-plugin2-1.0.0.jar] resolvedPlugins.PluginWrapper=PluginWrapper [descriptor=PluginDescriptor [pluginId=welcome-plugin, pluginClass=dyyx.pf4jdemo.core.plugin1.WelcomePlugin, version=1.0.0, provider=dugang, dependencies=[], description=, requires=*, license=null], pluginPath=/PATH/pf4jdemo-plugin1-1.0.0.jar] resolvedPlugins.PluginWrapper=PluginWrapper [descriptor=PluginDescriptor [pluginId=welcome-plugin2, pluginClass=dyyx.pf4jdemo.core.plugin2.Welcome2Plugin, version=1.0.0, provider=dugang, dependencies=[], description=, requires=*, license=null], pluginPath=/PATH/pf4jdemo/app/plugins/pf4jdemo-plugin2-1.0.0.jar] greetings plugin num 2 dyyx.pf4jdemo.core.plugin1.WelcomeGreeting@41975e01#Welcome dyyx.pf4jdemo.core.plugin2.Welcome2Greeting@c2e1f26#Welcome2 Welcome2Plugin.stop() WelcomePlugin.stop()
完整代码 https://gitee.com/dyyx/hellocode/tree/master/demo/pf4jdemo

上一篇     下一篇
scala Int 与 Integer的区别

pf4j 例子说明

Maven 打包 定制 manifest

Jooq 简介及实例

sbt 笔记

scala sbt 项目增加子项目