Spring Boot 中用于模块化应用程序的插件系统 [英] Plugin System in Spring Boot for modular applications

查看:21
本文介绍了Spring Boot 中用于模块化应用程序的插件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编译后寻找在 spring boot 中动态加载 jar,例如我将 jars 放在某个文件夹中,当 spring boot 启动时,该文件夹中的所有 jars 将注入 spring boot app.我不知道如何使用 spring boot 来做到这一点,如果你知道可以帮助我,举个例子.

I looking for dynamically loading jar in spring boot after compiling, for example I will put jars in some folder and when spring boot is started, all jars from this folder will be injected into spring boot app. I don't know how can I do this with spring boot, and if You know can help me with this, with some example.

我需要这个 jars 来拥有 @Service、@Controller,因为这将是模块(插件),为我的 Spring Boot 应用程序添加功能.

I need this jars to have @Service, @Controller as this will be module (plugin), with adding capabilities to my spring boot app.

使用 spring boot 可以做到这一点,如果可以,请提供一些示例代码.

Is possible to do this with spring boot, and if it is possible, please provide me with some sample code.

提前致谢.

更新:我发现了一些https://www.youtube.com/watch?v=F-sw2pFdcDw https://code.google.com/p/jspf/

更新 2:我无法从 Spring Boot 中注册的插件 jar 中获取 @Controller bean

UPDATE 2: I can't get @Controller bean from plugin jar registered in Spring Boot

推荐答案

看看 FlexiCore,一个开源框架,它利用在运行时加载的插件(jars)为 spring boot 带来模块化,参见 wizzdi 和 FlexiCore.例如,FlexiCore 允许您创建一个包含 spring bean 的项目(从主应用程序编译成一个单独的 jar),如下所示:

Have a look at FlexiCore, an open-source framework that brings modularity to spring boot utilizing plugins(jars) loaded at runtime See wizzdi and FlexiCore. for example FlexiCore allows you to create a project ( compiled into a seperate jar from your main application) that contains a spring bean as follows:

@Component
@Extension
public class HelloWorldService implements ServicePlugin{

 public String hello() {
  return "Hello World!";
 }

}

一旦放置在指定的插件文件夹中,它将自动加载,它基本上允许对大多数(所有)spring boot 功能的完全支持,因此例如,您也可以将 RestController bean 添加到您的 jar 中,FlexiCore 将自动加载该 bean,允许您像在主应用程序 jar 中一样调用控制器:

it will be automatically be loaded once placed inside the designated plugins folder, it basically allows a full support for most(all) of spring boot features , so for example you can add a RestController bean to your jar as well , FlexiCore will automatically load that bean allowing you to call the controller as if it was in your main application jar:

@RestController
@Extension
public class TestEntityController implements Plugin {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @Autowired
    private TestEntityService testEntityService;

    @PostMapping("/createTestEntity")
    public TestEntity createTestEntity(@RequestParam(name="name", required=false, defaultValue="Stranger") String name) {
        return testEntityService.createTestEntity(name);
    }

    @GetMapping("{id}")
    public TestEntity getTestEntity(@PathVariable("id")String id) {
        return testEntityService.getTestEntity(id);
    }

}

免责声明:我是 wizzdi 的首席技术官,该公司为 FlexiCore 提供支持.

Disclaimer: I am the CTO of wizzdi, the company powering FlexiCore.

这篇关于Spring Boot 中用于模块化应用程序的插件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆