Fork me on GitHub

An extension can also be defined directly in the application jar (i.e., you’re not obligated to put the extension in a plugin — you can see this extension as a default, or system extension). See WhazzupGreeting for a real example.

This is great for starting application phase. In this scenario you have a minimalist plugin framework with one class loader (the application class loader), similar with Java ServiceLoader but with the following benefits:

  • no need to write provider-configuration files in the resource directory META-INF/services, you using the elegant @Extension annotation from PF4J
  • anytime you can switch to the multiple class loader mechanism with no code changes in your application

The code present in the Boot class from the demo application is functional but you can use a more minimalist code skipping pluginManager.loadPlugins() and pluginManager.startPlugins().

public static void main(String[] args) {
    PluginManager pluginManager = new DefaultPluginManager();
    pluginManager.loadPlugins();
    pluginManager.startPlugins();
    List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
    for (Greeting greeting : greetings) {
        System.out.println(">>> " + greeting.getGreeting());
    }
}

The above code can be written:

public static void main(String[] args) {
    PluginManager pluginManager = new DefaultPluginManager();
    List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
    for (Greeting greeting : greetings) {
        System.out.println(">>> " + greeting.getGreeting());
    }
}