1.What is pf4j?
A plug-in framework for implementing plug-insDynamic loading, supported plug-in formats (zip, jar).
Core Components
- **Plugin: ** is the base class for all plugin types. Each plugin is loaded into a separate class loader to avoid conflicts.
- **PluginManager: ** Used for all aspects of plugin management (load, start, stop). You can use the built-in implementation as JarPluginManager, ZipPluginManager, DefaultPluginManager (it is a JarPluginManager + ZipPluginManager), or you can implement a custom plugin manager from AbstractPluginManager (implementing factory methods only).
- **PluginLoader: **All information (classes) required to load the plugin.
- **ExtensionPoint: ** is a point in the application where custom code can be called. This is a java interface tag. Any java interface or abstract class can be marked as an extension point (implementing the ExtensionPoint interface).
- **Extension: ** is the implementation of the extension point. It is a Java comment on a class
Scene
There is onespring-boot
The implemented web application provides an extension point on a certain business function. Users can implement function extension based on the SDK, require that plug-ins can be managed, and functions can be loaded dynamically at the business function extension point.
2. Code Engineering
Purpose of experiment
Implement dynamic loading of plug-ins, call unloading
Demo overall architecture
- pf4j-api: defines an extensible interface.
- pf4j-plugins-01: Plugin project, can contain multiple plugins, and the interface defined in plugin-api is required. All plug-in jar packages are placed in a unified folder for easy management. In the future, you only need to load the file directory path to start the plug-in.
- pf4j-app: The main program needs to rely on pf4j-api, load and execute pf4j-plugins-01.
pf4j-api
Import dependencies
<dependency> <groupId>org.pf4j</groupId> <artifactId>pf4j</artifactId> <version>3.0.1</version> </dependency>
Custom extension interface, integrated ExtensionPoint, marked as extension point
package .pf4j; import org.; public interface Greeting extends ExtensionPoint { String getGreeting(); }
Package for other projects
pf4j-plugins-01
If you want to be able to control the life cycle of the plugin, you can customize the method inside the class integration plugin
/* * Copyright (C) 2012-present the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * /licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.; import .; import ; import org.; import org.; import org.; import org.; /** * @author Decebal Suiu */ public class WelcomePlugin extends Plugin { public WelcomePlugin(PluginWrapper wrapper) { super(wrapper); } @Override public void start() { ("()"); // for testing the development mode if ((())) { (("WelcomePlugin")); } } @Override public void stop() { ("()"); } @Extension public static class WelcomeGreeting implements Greeting { @Override public String getGreeting() { return "Welcome ,my name is pf4j-plugin-01"; } } }
Make it into a jar or zip package to facilitate the loading of the main program
pf4j-app
Loading plugin packages
package .pf4j; import org.; import org.; import ; import ; import ; @SpringBootApplication public class DemoApplication { /* public static void main(String[] args) { (, args); }*/ 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 //(); (("D:\\IdeaProjects\\ETFramework\\pf4j\\pf4j-plugin-01\\target\\pf4j-plugin-01-1.")); (); /* // retrieves manually the extensions for the extension point List<Greeting> greetings = (); ("() = " + ()); */ // retrieve all extensions for "Greeting" extension point List<Greeting> greetings = (); for (Greeting greeting : greetings) { (">>> " + ()); } // stop and unload all plugins (); //(); } }
3. Test
Run the mian function inside to see the plug-in loading, calling and uninstalling situations
4. Quotation
/
/pf4j/pf4j-spring
This is the article about the code examples of SpringBoot integrating PF4j to implement plug-in development functions. For more related SpringBoot pf4j plug-in development content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!