java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot pf4j插件开发

SpringBoot集成pf4j实现插件开发功能的代码示例

作者:HBLOG

pf4j是一个插件框架,用于实现插件的动态加载,支持的插件格式(zip、jar),本文给大家介绍了SpringBoot集成pf4j实现插件开发功能的示例,文中通过代码示例给大家讲解的非常详细,需要的朋友可以参考下

1.什么是pf4j?

一个插件框架,用于实现插件的动态加载,支持的插件格式(zip、jar)。

核心组件

场景

有一个spring-boot实现的web应用,在某一个业务功能上提供扩展点,用户可以基于SDK实现功能扩展,要求可以管理插件,并且能够在业务功能扩展点处动态加载功能。

2.代码工程

实验目的

实现插件动态加载,调用 卸载

Demo整体架构

pf4j-api

导入依赖

<dependency>
    <groupId>org.pf4j</groupId>
    <artifactId>pf4j</artifactId>
    <version>3.0.1</version>
</dependency>

自定义扩展接口,集成 ExtensionPoint ,标记为扩展点

package com.et.pf4j;

import org.pf4j.ExtensionPoint;

public interface Greeting extends ExtensionPoint {

    String getGreeting();

}

打包给其他项目引用

pf4j-plugins-01

如果你想要能够控制插件的生命周期,你可以自定义类集成 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
 *
 *     http://www.apache.org/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.pf4j.demo.welcome;

import com.et.pf4j.Greeting;
import org.apache.commons.lang.StringUtils;

import org.pf4j.Extension;
import org.pf4j.Plugin;
import org.pf4j.PluginWrapper;
import org.pf4j.RuntimeMode;

/**
 * @author Decebal Suiu
 */
public class WelcomePlugin extends Plugin {

    public WelcomePlugin(PluginWrapper wrapper) {
        super(wrapper);
    }

    @Override
    public void start() {
        System.out.println("WelcomePlugin.start()");
        // for testing the development mode
        if (RuntimeMode.DEVELOPMENT.equals(wrapper.getRuntimeMode())) {
            System.out.println(StringUtils.upperCase("WelcomePlugin"));
        }
    }

    @Override
    public void stop() {
        System.out.println("WelcomePlugin.stop()");
    }

    @Extension
    public static class WelcomeGreeting implements Greeting {

        @Override
        public String getGreeting() {
            return "Welcome ,my name is pf4j-plugin-01";
        }

    }

}

打成jar或者zip包,方便主程序加载

pf4j-app

加载插件包

package com.et.pf4j;

import org.pf4j.JarPluginManager;
import org.pf4j.PluginManager;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.nio.file.Paths;
import java.util.List;

@SpringBootApplication
public class DemoApplication {

/* public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, 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
      //pluginManager.loadPlugins();

      pluginManager.loadPlugin(Paths.get("D:\\IdeaProjects\\ETFramework\\pf4j\\pf4j-plugin-01\\target\\pf4j-plugin-01-1.0-SNAPSHOT.jar"));
      pluginManager.startPlugins();
      /*
        // retrieves manually the extensions for the Greeting.class extension point
        List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
        System.out.println("greetings.size() = " + greetings.size());
        */
      // 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();


   }
}

3.测试

运行DemoApplication.java 里面的mian函数,可以看到插件加载,调用以及卸载情况

4.引用

https://pf4j.org/

https://github.com/pf4j/pf4j-spring

到此这篇关于SpringBoot集成pf4j实现插件开发功能的代码示例的文章就介绍到这了,更多相关SpringBoot pf4j插件开发内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文