Fork me on GitHub

To create extensions instances, PF4J uses an ExtensionFactory. By default, PF4J uses DefaultExtensionFactory as implementation of ExtensionFactory.

You can change the default implementation with:

new DefaultPluginManager() {
    
    @Override
    protected ExtensionFactory createExtensionFactory() {
        return MyExtensionFactory();
    }

};

DefaultExtensionFactory uses Class#newInstance() method to create the extension instance.

An extension instance is created on demand, when plugin.getExtensions(MyExtensionPoint.class) is called. By default, if you call plugin.getExtensions(MyExtensionPoint.class) twice:

plugin.getExtensions(MyExtensionPoint.class);
plugin.getExtensions(MyExtensionPoint.class);

then for each call, a new extension instance is created.

If you want to return the same extension instance (singleton), you need to use SingletonExtensionFactory:

new DefaultPluginManager() {
    
    @Override
    protected ExtensionFactory createExtensionFactory() {
        return SingletonExtensionFactory();
    }

};