Git Product home page Git Product logo

Comments (16)

laurentkvb avatar laurentkvb commented on June 10, 2024 1

Hi, I am able to share some of the used source code to make the controllers work in Spring Boot. I'll get back at it. Maybe it can be used as a new sub project of the PF4J especially for Spring Boot.

from pf4j-spring.

decebals avatar decebals commented on June 10, 2024

Maybe below links can help you:

from pf4j-spring.

Julieng50 avatar Julieng50 commented on June 10, 2024

I already tried that, but my class annotated @controller is not working.
This is not very clear on how to implement Component with PF4J, in particular for @controller (with @RequestMapping).

If someone got a working project to show us how you did that, maybe we could do a easier way to use PF4J with Spring.

from pf4j-spring.

decebals avatar decebals commented on June 10, 2024

The idea is that PF4J loads each plugin with different Java class loader.
So, it's important to know in what Spring (application) context your controller is defined. Is defined in the plugin's application context or in the big application context?

from pf4j-spring.

Julieng50 avatar Julieng50 commented on June 10, 2024

I have controller in both, but the one that does not work is in the plugin, so defined in with de plugin's application context.
The thing that I'm trying to do : make my big application loads dynamically Controllers (from plugins).

from pf4j-spring.

decebals avatar decebals commented on June 10, 2024

Spring comes with an hierarchically application context concept. In your case, the beans registered via plugin's application context is not visible in the big application context and vice versa.
In my example with HelloPlugin, I set the big application context as parent for the plugin application context. In this mode I have access from plugin to the beans defined in the big application context.
The pf4j-spring library exports extensions as beans. After this, you can use autowire to inject extensions in a spring aware class (see https://github.com/decebals/pf4j-spring/blob/scope/demo/app/src/main/java/org/pf4j/demo/Greetings.java#L30).
I don't know what can I say about your use case.

from pf4j-spring.

Julieng50 avatar Julieng50 commented on June 10, 2024

Ok, I finally find a way to do that :
In API :

public interface GenericController extends ExtensionPoint {
}

In Plugin :

@Extension
@RestController
public class PluginController implements GenericController {

    @GetMapping("/abc")
    public Object home() {
        return "OK";
    }

}

No need to add applicationContext.register(PluginController.class); in the createApplicationContext() in the plugin.

If someone find a way to do it wihtout @Extension... :)

Edit : I wrote this before your reply, I understand now that Extensions are injected in spring beans and using "register" does not work as I expected, thanks :)

from pf4j-spring.

fhchina avatar fhchina commented on June 10, 2024

I have the same requirements

from pf4j-spring.

decebals avatar decebals commented on June 10, 2024

A good introduction to the problem (Spring and modularity) you can find in A Modular Architecture with Spring Boot. In that article you can read about hierarchical application context and that a child context can access bean from the parent context but not vice versa.
You can see the @Exported annotation from that article like something similar with @Extension annotation from PF4J (both annotations register an object as singleton bean in the parent context).

The solution of @Julieng50 with @Extension on the controller class seems to be good and natural for me. In the end your controller implementation is an extension of controller extension point.
Maybe @Julieng50 can show us a functional demo application that integrate Spring (MVC or Boot) and PF4J.

from pf4j-spring.

decebals avatar decebals commented on June 10, 2024

In the end if you don't use the extension mechanism from PF4J, what are you using from PF4J, only the plugin mechanism (load and start plugins)?
I think that would be possible in the future to use the @Controller, @Service, @Component annotations from Spring, with PF4J-Spring without to use the @Extension annotation from PF4J.

I wish to write below some theoretical aspects related to this subject.
In Spring we can have hierarchical application context, you can visualize these contexts as nodes in a tree (a hierarchy), a node (application context) could have one parent, siblings and children.
In PF4J each plugin contain other plugins as dependencies and also it can be a dependency for other plugins. You can visualize these plugins as node in a (directed) graph.
I think in the end that it's possible to traverse the dependencies graph from PF4J (it's available), and to create a Spring hierarchical application context for each Spring plugin (node) from the graph. We can add the application context of dependency plugin as parent context and the application context of dependent plugin (other plugins that depends on our plugin) as children context. It's a problem here that in Spring hierarchical application context we cannot have multiple parents.

Example:
P1 -> P2 -> P3 (plugin P1 is a dependency for P2 and P2 is a dependency for P3).
Each plugins is a Spring plugin (it comes with an application context). When we instantiate P3 we can specify that it application context has as parent the application context of P2, in this mode, all beans defined in P2 are available for P3 (but not vice versa).

from pf4j-spring.

laurentkvb avatar laurentkvb commented on June 10, 2024

@Julieng50 Can you share the source code of your plugin and Spring application or atleast the main class?

from pf4j-spring.

llatasa avatar llatasa commented on June 10, 2024

Hi,

yes, it would be cool if @Julieng50 shared the project (or a working sample). I am using that approach and the constructor of my RestController is being executed, but then when I type the URL in the browser I get a 404 error, so I guess I'm missing something :-D.

Anybody else is having the same issue?

from pf4j-spring.

decebals avatar decebals commented on June 10, 2024

Can we close this issue?

from pf4j-spring.

llatasa avatar llatasa commented on June 10, 2024

I haven't been able to get it working. I used @Julieng50 comment but I always get a 404 error.

I would like to use the framework in my projects and use the new version is l of spring boot, but it is not easy to get a project with a data source in the main context, rest controllers in the plugins and so on.

Do you plan to publish a whole example?

Regards :-)

from pf4j-spring.

decebals avatar decebals commented on June 10, 2024

Maybe the same problem is encountered in #18.
I see people that use PF4J with Spring (Boot) but I don't know why people using these two projects not share knowledge/projects .
I don't use Spring in my projects, so I cannot help more in this field.
Maybe someone with strong Spring knowledge will help us to move this module (pf4j-spring) forward.
I hope πŸ˜„ .

from pf4j-spring.

dtrunk90 avatar dtrunk90 commented on June 10, 2024

EDIT: moved to #27

An example how to use @Entity annotated (JPA/Hibernate) POJO classes in a plugin while the main app is a spring boot application (auto configuration; see JpaBaseConfiguration#getPackagesToScan() would be nice, too.

Cause as my plugin has different package names and another class loader they won't be picked up by getPackagesToScan
I guess you need to create your own LocalContainerEntityManagerFactoryBean bean to call setPackagesToScan yourself. But how to pick up all the plugin packages from pluginManager bean?

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.