Awesome Open Source
Awesome Open Source

PF4J - Spring Framework integration

Travis CI Build Status Maven Central

This project is a proof of concept related to how you can integrate PF4J with Spring Framework.


  • ExtensionsInjector allows PF4J's extensions to be expose as Spring beans.
  • SpringPlugin your plugin extends this class if your plugin contains Spring beans
  • SpringExtensionFactory use this ExtensionFactory in your PluginManager if you have SpringPlugins
  • SpringPluginManager a Spring aware PluginManager

Using Maven

In your pom.xml you must define the dependencies to PF4J-Spring artifact with:


where ${pf4j-spring.version} is the last pf4j-spring version.

You may want to check for the latest released version using Maven Search

Also you can use the latest SNAPSHOT via the Sonatype Maven Repository. For this, you must add above lines in your pom.xml:


How to use

Create the Spring configuration (declare some beans) using annotations with:

public class SpringConfiguration {

    public SpringPluginManager pluginManager() {
        return new SpringPluginManager();

    public Greetings greetings() {
        return new Greetings();


SpringExtensionFactory creates a new extension instance every time a request is done.
If you want a singleton extension instance please use SingletonSpringExtensionFactory that always returns a specific instance. Optional you can specify the extension classes for which you want singletons.

Start your application (plain java code):

public class Boot {

    public static void main(String[] args) {
        // retrieves the Spring application context
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);

        // retrieves automatically the extensions for the Greeting.class extension point
        Greetings greetings = applicationContext.getBean(Greetings.class);

        // stop plugins
        PluginManager pluginManager = applicationContext.getBean(PluginManager.class);
        // retrieves manually the extensions for the Greeting.class extension point
        List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
        System.out.println("greetings.size() = " + greetings.size());


Consume the PF4J extensions as Spring beans:

public class Greetings {

    private List<Greeting> greetings;

    public void printGreetings() {
        System.out.println(String.format("Found %d extensions for extension point '%s'", greetings.size(), Greeting.class.getName()));
        for (Greeting greeting : greetings) {
            System.out.println(">>> " + greeting.getGreeting());


The output is:

Found 2 extensions for extension point 'org.pf4j.demo.api.Greeting'
>>> Welcome
>>> Hello

Bellow I present you a more complex example where a plugin (see demo plugin2 - HelloPlugin) uses Spring Framework internally.

First, create an interface MessageProvider with an implementation class HelloMessageProvider

public interface MessageProvider {

    String getMessage();


public class HelloMessageProvider implements MessageProvider {

    public String getMessage() {
        return "Hello";


Declare the plugin's beans via Spring Configuration

public class SpringConfiguration {

    public MessageProvider messageProvider() {
        return new HelloMessageProvider();


Create my (Spring) plugin

public class HelloPlugin extends SpringPlugin {

    public HelloPlugin(PluginWrapper wrapper) {

    public void start() {

    public void stop() {
        super.stop(); // to close applicationContext

    protected ApplicationContext createApplicationContext() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        return applicationContext;

    public static class HelloGreeting implements Greeting {

        private MessageProvider messageProvider;

        public String getGreeting() {
//            return "Hello";
            // complicate a little bit the code
           return messageProvider.getMessage();



Ready, your extension is available in your application via PluginManager or Spring Autowire.

For more details please see the demo application.

Implementation details

ExtensionsInjector injects each PF4J's extension as a bean in Spring Framework. For example if you run the demo application you will see these lines in log:

2014-06-16 16:40:36,573 DEBUG org.pf4j.spring.ExtensionsInjector - Registering extensions of the plugin 'welcome-plugin' as beans
2014-06-16 16:40:36,586 DEBUG org.pf4j.spring.ExtensionsInjector - Register extension 'org.pf4j.demo.welcome.WelcomePlugin$WelcomeGreeting' as bean
2014-06-16 16:40:36,589 DEBUG org.pf4j.spring.ExtensionsInjector - Registering extensions of the plugin 'hello-plugin' as beans
2014-06-16 16:40:36,589 DEBUG org.pf4j.spring.ExtensionsInjector - Register extension 'org.pf4j.demo.hello.HelloPlugin$HelloGreeting' as bean

The bean name is the extension class name (for example 'org.pf4j.demo.welcome.WelcomePlugin$WelcomeGreeting').

For more information please see the demo sources.


I have a tiny demo application. The demo application is in demo package.

Run the pf4j-spring demo (Boot class contains the main method) from IDE (IntelliJ in my case) with these arguments as VM options:


and working directory:

Alternatives To Pf4j Spring
Select To Compare

Alternative Project Comparisons
Related Awesome Lists
Top Programming Languages

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Java (404,612
Plugin (113,427
Spring (28,718
Spring Framework (1,070
Modularity (587
Pf4j (9