Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support constructor injection in extensions #50

Merged
merged 3 commits into from
Oct 29, 2020
Merged

Support constructor injection in extensions #50

merged 3 commits into from
Oct 29, 2020

Conversation

m-schroeer
Copy link
Contributor

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();
    }
}

…entations

The implementation follows the former one in getting the best fitting spring applciation context.
After that (if present) the application context is used to create an autowired bean by using mode 'AUTOWIRE_CONSTRUCTOR' and autowire the rest (setters, fields) right after having the instance.

If no context could be received (non spring plugin and non SpringPluginManager (unusual with SpringExtensionFactory but possible)) default Java reflection is used like before except that it public support constructors that have a non empty parameter list as well.
@m-schroeer m-schroeer changed the title Support constructor injection in extensions (implementation of ExtensionPoint) Support constructor injection in extensions Jul 19, 2020
@decebals
Copy link
Member

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

@m-schroeer
Copy link
Contributor Author

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
Copy link
Member

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

@decebals decebals merged commit 2282e1c into pf4j:master Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants