我正在开发一个可以使用非弹簧插件的多模块Spring Boot项目。使用java.util.ServiceLoader
加载插件。他们可能会使用实现org.springframework.context.ApplicationContextAware
的ContextBridge提供的spring bean。最终结果如下:
app-folder/
plugins/
myplugin.jar
myapplication.jar
launcher-script.sh
使用shell脚本启动时,应用程序和插件按预期工作:
java -Dloader.path=.,plugins -jar myapplication.jar "$@"
但是,我无法弄清楚如何配置IntelliJ来启动应用程序并找到插件,这样我就可以更舒适地进行调试。
我尝试了以下方法
-Dloader.path
配置添加到启动配置似乎不起作用。我已经尝试了Appliaction和Spring Boot启动配置。这似乎没有做任何事情。它只加载主应用程序但没有插件。通过gradle添加插件:
compile fileTree(dir: 'dist/plugins', include: '**/*.jar')
这会加载插件,但无法加载我从错误消息中获取的上下文:
s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myServiceImpl': Unsatisfied dependency expressed through method 'setMyService' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.myapp.MyService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
所以我在代码中添加了bean依赖项,以强制bean初始化的某个顺序:
数据访问模块
interface Myservice {
...
}
@Service
@Qualifier("MyServiceBean")
class MyServiceImpl implements MyService {
...
}
插件内的ContextBridge
@Component
@DependsOn("MyService)
public class ContextBridgeImpl implements ContextBridge, ApplicationContextAware {
...
}
然后我得到:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'MyService' available
让我感到困惑的是,从控制台启动时,所有事情都会起作用。为什么不在IntelliJ里面?