pf4j / pf4j-spring

Plugin Framework for Spring (PF4J - Spring Framework integration)
Apache License 2.0
346 stars 105 forks source link

Support constructor injection in extensions #50

Closed m-schroeer closed 4 years ago

m-schroeer commented 4 years ago

Solves #49

As a result we can use all kinds of dependency injection in our extension classes (as long as the dependencies are spring managed beans)

@Extension
public class Foo implements ExtensionPoint {

    private final Bar bar;       // Constructor injection
    private Baz baz;             // Setter injection
    @Autowired
    private Qux qux;             // Field injection

    @Autowired
    public Foo(final Bar bar) {
        this.bar = bar;
    }

    @Autowired
    public void setBaz(final Baz baz) {
        this.baz = baz;
    }
}

@Configuration
public class FooConfiguration {

    @Bean
    public Bar bar() {
        return new Bar();
    }

    @Bean
    public Baz baz() {
        return new Baz();
    }

    @Bean
    public Qux qux() {
        return new Qux();
    }
}
decebals commented 4 years ago

@m-schroeer Can you fix the conflict please? I want to merge this PR. Thanks!

m-schroeer commented 4 years ago

Done. But first I was confused what caused the conflicts as I did not noticed changes made by #51. Maybe @lhanda should have a look at the code again as the changes are not considered in my commits and would be replaced. And currently I am not in depth into the code and don't have the time to reconsider it by myself, at least in the next two weeks.

From a first look at #51 its intention reminds me of difficulties I had by myself when retracing which bean (of plugins and system) is assigned to which context. I'm not 100% sure if #51 tackles the problem of my memory, I just want to say that I think its worth a look again to have a cleaner way of creating spring beans (only one instance and instead of two or more).

decebals commented 4 years ago

@lhanda What do you think about what @m-schroeer says? Do you see a problem?