Giter VIP home page Giter VIP logo

Comments (13)

decebals avatar decebals commented on July 17, 2024

I don't use Spring in my projects so I don't use this PF4J's extension (pf4j-spring).
This project is very tiny and easy to hack. All the business is available in two classes: SpringExtensionFactory and ExtensionsInjector.
My idea is to add a new annotation (for example @SpringScope) on class annotated with @extension. In this mode if you have the class (name) of extension you can read the value from @SpringScope.
All you need to do is to modify SpringExtensionFactory or ExtensionsInjector and to use the scope value when register the extension as Spring's bean.

Make sense for you?

from pf4j-spring.

hnrkdmsk avatar hnrkdmsk commented on July 17, 2024

You mean something like this?

protected void registerExtension(Object extension, BeanDefinitionRegistry beanDefinitionRegistry) {
        BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(extension.getClass())
            .setScope(extension.getClass().getAnnotation(SomeSpringScopeAnnotation.class).value()) // here make use of the new added annotation e.g. @springscope
            .addPropertyReference("propertyName", extension.getClass().getName())
            .addPropertyValue("propertyValue", extension);
        beanDefinitionRegistry.registerBeanDefinition(extension.getClass().getName(), definitionBuilder.getBeanDefinition());
}

There I have edit the ExtensionsInjector. I don't tested it yet and don't know if this work, but it can be an idea for it. The biggest change would be the switch from the SingleBeanRegistry to BeanDefinitionRegistry.

@PostConstruct
public void injectExtensions(BeanDefinitionRegistry beanDefinitionRegistry) {
...
}

from pf4j-spring.

decebals avatar decebals commented on July 17, 2024

Yes. Something like this.

from pf4j-spring.

decebals avatar decebals commented on July 17, 2024

What it's not clear for me is the comment from 1139d13#comments that says that

PostConstruct cannot work with parameters of the method

Can you validate this affirmation. Did you able to run the demo without problem?

The modification with ExtensionsInjector.injectExtensions(SingletonBeanRegistry beanRegistry) is an attempt to solve #3.

Thanks!

from pf4j-spring.

decebals avatar decebals commented on July 17, 2024

I see that exists org.springframework.context.annotation.Scope. It's an idea to use this annotation.

from pf4j-spring.

hnrkdmsk avatar hnrkdmsk commented on July 17, 2024

org.springframework.context.annotation.Scope would be great for this!

I can get it running, but not fully. The application context of the plugin is not resolved anymore. But with my fix, the scope will be set right.

protected void registerExtension(Object extension, BeanDefinitionRegistry beanDefinitionRegistry) {
        BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(extension.getClass())
            .addPropertyReference("propertyName", extension.getClass().getName())
            .addPropertyValue("propertyValue", extension);

        if (extension.getClass().isAnnotationPresent(ExtensionSpringScope.class)) {
            String scope = extension.getClass().getAnnotation(ExtensionSpringScope.class).value();
            log.info("Registering {} with scope: {}", extension.getClass().getName(), scope);
            definitionBuilder.setScope(scope);
        }

        beanDefinitionRegistry.registerBeanDefinition(extension.getClass().getName(), definitionBuilder.getBeanDefinition());
    }

What I had to change was the @PostConstruct method.

@Autowired
    private ApplicationContext applicationContext;

    public ExtensionsInjector(PluginManager pluginManager) {
        this.pluginManager = pluginManager;
    }

    @PostConstruct
    public void injectExtensions() {
      BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory();
...}

The post construct method don't allow parameters anymore. I don't checked if this has a relation to my changes. But now I don't get my beans from the plugin loaded... Can you check it?

from pf4j-spring.

decebals avatar decebals commented on July 17, 2024

Can you fork the project (via GitHub), add your modifications to pippo-spring and give me the link?

from pf4j-spring.

hnrkdmsk avatar hnrkdmsk commented on July 17, 2024

Yes, I can! Here: https://github.com/loefflefarn/pf4j-spring/tree/pippo-spring

from pf4j-spring.

decebals avatar decebals commented on July 17, 2024

@loefflefarn
I made some modifications on master to organize better the code and to prepare things for Scope implementation.
I created a new branch "scope" that contains all modifications. I added scope "prototype" on one of extension from demo and it works. I will create a PR to have the possibility to review the code and add comments.

from pf4j-spring.

hnrkdmsk avatar hnrkdmsk commented on July 17, 2024

Nice, okay. I will check it tomorow in my application and will give you feedback! Thanks a lot for you help.

from pf4j-spring.

hnrkdmsk avatar hnrkdmsk commented on July 17, 2024

The scopes are now setted right but they cannot resolve there beans from the plugin. You can test it, if you set the scope to prototype in the demo application-plugin2.

from pf4j-spring.

decebals avatar decebals commented on July 17, 2024

@loefflefarn
I explained yesterday in some comments of PR #6 why this problem appear.

from pf4j-spring.

hnrkdmsk avatar hnrkdmsk commented on July 17, 2024

Ah, yes. Okay, I will look around for a possible solution to help you with it. I've tested around with this, but nothing has work fine.

from pf4j-spring.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.