我在编译后寻找在 spring boot 中动态加载 jar,例如我将 jar 放在某个文件夹中,当 spring boot 启动时,该文件夹中的所有 jar 将被注入(inject)到 spring boot 应用程序中。我不知道如何使用 spring boot 执行此操作,如果您知道可以通过一些示例帮助我。
我需要这个 jars 有@Service、@Controller,因为这将是模块(插件),并为我的 spring boot 应用程序添加功能。
可以用 spring boot 做到这一点,如果可以的话,请提供一些示例代码。
提前致谢。
更新: 我发现了一些东西 https://www.youtube.com/watch?v=F-sw2pFdcDw https://code.google.com/p/jspf/
更新 2:我无法从在 Spring Boot 中注册的插件 jar 中获取 @Controller bean
最佳答案
看看FlexiCore ,一个开源框架,利用在运行时加载的插件(jars)为 spring boot 带来模块化参见 wizzdi和 FlexiCore。 例如 FlexiCore 允许您创建一个项目(从您的主应用程序编译到一个单独的 jar 中),其中包含一个 spring bean,如下所示:
@Component
@Extension
public class HelloWorldService implements ServicePlugin{
public String hello() {
return "Hello World!";
}
}
一旦放置在指定的插件文件夹中,它将自动加载,它基本上可以完全支持大多数(所有)spring boot 功能,因此例如您也可以将 RestController bean 添加到您的 jar 中,FlexiCore 将自动加载该 bean,允许您调用 Controller ,就好像它在您的主应用程序 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 提供支持。
关于spring - Spring Boot 中用于模块化应用程序的插件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25751988/