Implementation and use of plug-ins


Create a plug-in startup class by declaring the @Plugin annotation on a class that implements the IPlugin interface, which will be loaded and managed by the plug-in factory. A plug-in package can include multiple plug-in startups Class, each plug-in startup class can implement its own business interface to provide external services;

  • @Plugin Annotation parameter description:

    id: The unique ID of the plug-in. If not filled in, the MD5 encrypted value of the initialization class name will be used as the ID;

    name: The plug-in name, the default is "";

    alias: The plug-in alias , the default is "";

    author: the plug-in author, the default is "";

    email: the contact email, the default is "";

    version: the plug-in version, the default Is "1.0.0";

    automatic: whether to automatically start running after loading, the default is true;

    description: plug-in description, the default is "";

  • IPlugin interface method description:

    init: plug-in initialization;

    getPluginContext: returns the plug-in environment context object;

    isInited: returns whether the plug-in is Initialized;

    isStarted: Returns whether the plug-in has been started;

    startup: Starts the plug-in;

    shutdown: Stops the plug-in;

    destroy: Destroys the plug-in object ;

The plug-in framework provides an AbstractPlugin abstract class that encapsulates the IPlugin interface. It is recommended to inherit it directly. Sample code:

@Plugin
public class DemoPlugin extends AbstractPlugin {
    // 根据需要重写父类方法...
}

Example of plug-in combined with business interface:

// 定义一个业务接口
public interface IBusiness {
    void sayHi();
}

@Plugin(id = "demo_plugin",
        name = "DemoPlugin",
        author = "有理想的鱼",
        email = "suninformaiton#163.com",
        version = "1.0")
public class DemoPlugin extends AbstractPlugin implements IBusiness {

    @Override
    public void startup() throws Exception {
        super.startup();
        //
        System.out.println("started.");
    }

    @Override
    public void shutdown() throws Exception {
        super.shutdown();
        //
        System.out.println("shutdown.");
    }

    public void sayHi() {
        System.out.println("Hi, from Plugin.");
    }
}

##Usage of plug-in

We have already Created a DemoPlugin plug-in and implemented the IBusiness business interface. The following describes how to use the plug-in and call the business interface method:

public static void main(String[] args) throws Exception {
    YMP.get().init();
    try {
        DemoPlugin _plugin = (DemoPlugin) Plugins.get().getPluginFactory().getPlugin("demo_plugin");
        // 或者 
        // _plugin = Plugins.get().getPluginFactory().getPlugin(DemoPlugin.class);
        //
        _plugin.sayHi();
        //
        IBusiness _business = Plugins.get().getPluginFactory().getPlugin(IBusiness.class);
        _business.sayHi();
    } finally {
        YMP.get().destroy();
    }
}
Execution result:

Hi, from Plugin.
Hi, from Plugin.
shutdown.

Note: The same plug-in can implement multiple business interfaces. If multiple plug-ins implement the same business interface, according to the plug-in loading order, the last one loaded The plug-in instance object will replace the former;