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

Injecting beans from plugin in application doesn't work #69

Open
daincredibleholg opened this issue Sep 20, 2021 · 2 comments
Open

Injecting beans from plugin in application doesn't work #69

daincredibleholg opened this issue Sep 20, 2021 · 2 comments

Comments

@daincredibleholg
Copy link

Hi.

We are currently building a Spring Boot-based service that will use plugins to make it highly customizable for our users.

One thing we try to do is providing beans via plugins that can be injected by the host application.

From what I found so far, it looks like the plugin manager isn't instantiated yet when Spring tries to resolve all injectable dependencies.
To make this (hopefully) a bit easier to understand, here is an example project

In the given example, I try to inject a bean from the plugin into the main application. This results in the application not even starting, as Spring doesn't seem to find the bean it needs to inject.

Later, we would like to use plugins to provide database connections. So, we would need @Configuration classes and injection working within and from the plugin.

Is this possible? Are there examples I can have a look at?

@jingsewu
Copy link

jingsewu commented Jan 4, 2024

Yes ,In my project. I can't obtain the bean from the spring contain too. My code is below:

@Autowired
private ApplicationContext applicationContext;

@Override
public void afterDoOperation(String string) {
    log.info("plugin logging:" + string);
    Object dictionaryController = applicationContext.getBean("dictionaryController");
    System.out.println(dictionaryController);
}

The dictionaryController is null. I think the reason is when we start the plugin we will
new AnnotationConfigApplicationContext(); .
so when we use @Autowired , it will query the bean from the new application context . not the main application context.

So you need to add the code:
applicationContext.setParent(((SpringPluginManager) getWrapper().getPluginManager()).getApplicationContext());
When you create the applicationContext in you SpringPlugin Class.

@decebals
Copy link
Member

decebals commented Jan 4, 2024

I don't use Spring with PF4J in my projects and from this reason I don't have a rich expertise on this subject.
Please read #27 (comment), and take a look on Spring ApplicationContext hierarchies.

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

No branches or pull requests

3 participants