在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:pf4j/pf4j开源软件地址:https://github.com/pf4j/pf4j开源编程语言:Java 99.3%开源软件介绍:Plugin Framework for Java (PF4J)A plugin is a way for a third party to extend the functionality of an application. A plugin implements extension points declared by application or other plugins. Also a plugin can define extension points. NOTE: Starting with version 0.9 you can define an extension directly in the application jar (you're not obligated to put the extension in a plugin - you can see this extension as a default/system extension). See WhazzupGreeting for a real example. Features/BenefitsWith PF4J you can easily transform a monolithic java application in a modular application. Practically PF4J is a microframework and the aim is to keep the core simple but extensible. I try to create a little ecosystem (extensions) based on this core with the help of the comunity.
No XML, only Java. You can mark any interface or abstract class as an extension point (with marker interface ExtensionPoint) and you specified that an class is an extension with @Extension annotation. Also, PF4J can be used in web applications. For my web applications when I want modularity I use pf4j-wicket. Components
PLUGIN = a container for EXTENSION POINTS and EXTENSIONS + lifecycle methods (start, stop, delete) A PLUGIN is similar with a MODULE from other systems. If you don't need lifecycle methods (hook methods for start, stop, delete) you are not forced to supply a plugin class (the How to useIt's very simple to add pf4j in your application. Define an extension point in your application/plugin using ExtensionPoint interface marker: public interface Greeting extends ExtensionPoint {
String getGreeting();
} Create an extension using @Extension
public class WelcomeGreeting implements Greeting {
public String getGreeting() {
return "Welcome";
}
} Create (it's optional) a public class WelcomePlugin extends Plugin {
public WelcomePlugin(PluginWrapper wrapper) {
super(wrapper);
// you can use "wrapper" to have access to the plugin context (plugin manager, descriptor, ...)
}
@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()");
}
} In above code I created a plugin (welcome) that comes with one extension for the You can distribute you plugin as a jar file (the simple solution). In this case add the plugin's metadata in
In above manifest I described a plugin with id Now you can play with plugins and extensions in your code: public static void main(String[] args) {
...
// create the plugin manager
PluginManager pluginManager = new JarPluginManager(); // or "new ZipPluginManager() / new DefaultPluginManager()"
// start and load all plugins of application
pluginManager.loadPlugins();
pluginManager.startPlugins();
// retrieve all extensions for "Greeting" extension point
List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
for (Greeting greeting : greetings) {
System.out.println(">>> " + greeting.getGreeting());
}
// stop and unload all plugins
pluginManager.stopPlugins();
pluginManager.unloadPlugins();
...
} The output is:
PF4J is very customizable and comes with a lot of goodies. Please read the documentation to discover yourself the power of this library. DocumentationDocumentation is available on pf4j.org DemoDemo applications are available in demo folder Quickstart (call to action)
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论