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

getting a single extension fails when multiple extensions exist even using Primary #38

Open
claymccoy opened this issue Nov 19, 2019 · 4 comments

Comments

@claymccoy
Copy link

I've got a more representative demo Spring PF4J demo here:
https://github.com/claymccoy/Pf4jSpringGreetingDemo

It works fine, even when I uncomment the single greeting endpoint.
https://github.com/claymccoy/Pf4jSpringGreetingDemo/blob/master/src/main/java/org/pf4j/demo/GreetingsController.java#L25

But with the single greeting endpoint uncommented, if I add more greeting extensions via plugins it fails. That is not surprising. However, I would expect it to work if one of the extensions is marked with the Primary Spring annotation. But I still get this:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field greeting in org.pf4j.demo.GreetingsController required a single bean, but 2 were found:
	- org.pf4j.demo.WhazzupGreeting: a programmatically registered singleton	- com.claymccoy.pf4jdemo.bjj.BjjGreeting: a programmatically registered singleton

Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

> Task :bootRun FAILED
@claymccoy claymccoy changed the title getting a single extension marked as Primary fails getting a single extension fails when multiple extensions exist even using Primary Nov 19, 2019
@decebals
Copy link
Member

I don't know how I can help here.
The pf4j-spring is very tiny and easy to hack. All the business is available in two classes: SpringExtensionFactory and ExtensionsInjector. The extension is injected as bean in SpringExtensionFactory#create.

I am not familiar with @primary annotation but it seems that the behavior of this annotation can be easy emulated via code:

Greeting getGreeting() {
    // pseudo code
    if (greetings.isEmpty) {
        return null;
    } else {
        return greetings.get(0); // first
    }
}

If you want something automatically (not the first greeting extension), I think that you can add @primary on an extension class and use this information in ExtensionsInjector. Please take a look on #6 to have an idea.

@decebals
Copy link
Member

Maybe this link helps.

@decebals
Copy link
Member

decebals commented Nov 23, 2019

In GreetingsController you say:

This works with just the system extension, but fails with more than one even when one is marked @Primary

Yes, this behavior is normal (with current implementation) because PF4J creates the extensions instances, and not Spring. In this context, any Spring's annotation that exists on the extension class (like Scope, Primary, ...) must be treated/interpreted by PF4J (pf4j-spring) when an extension's instance is created. After the extension's instance was created, the object is injected in Spring as bean.
If the extension's instance was created by Spring, then Spring would take care of all these aspects (annotations). So, the perfect/ideal solution is to let Spring to create these instances of extensions but I don't know how we can do this.

@decebals
Copy link
Member

Maybe an idea is to use the meta annotation concept from Spring (for more details see this).
On the master branch of PF4J there is already support for the extensively decorated annotation(pf4j/pf4j#347). So, right now, we can write something like:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Extension
@Component
public @interface SpringExtension {
}

where @Extension defines an extension in PF4J and @Component defines a component in Spring.
What else we should do is implement an ExtensionFactory (see SpringExtensionFactory) that extracts the extension instance from Spring on create(Class<T> extensionClass).

It's just an idea. It's not validated in practice.

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

2 participants