pf4j spring 集成几个问题
以下整理几个实际使用碰到的问题
问题
- Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/pf4j/PluginClassLoader) previously initiated loading for a different type with name "org/springframework/context/ApplicationContext"
移除spring core ,在插件项目中对于spring 的依赖设置为provide
- org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'jdbcTemplate' is expected to be of type 'org.springframework.jdbc.core.JdbcTemplate' but was actually of type 'org.springframework.jdbc.core.JdbcTemplate'
与上边一个问题一样,不然会有莫名的问题
- bean 注入问题
官方提供了SpringPluin 抽象 ,此类可以解决spring context 的创建以及一些依赖处理,一般的玩法是自己创建一个,同时对于需要
注意的beans在此对象中注册
参考
PluginBConfig 为一个Configuration ,可以声明我们需要的bean
public class PluginB extends SpringPlugin {
public PluginB(PluginWrapper wrapper) {
super(wrapper);
}
@Override
protected ApplicationContext createApplicationContext() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.setClassLoader(getWrapper().getPluginClassLoader());
applicationContext.register(PluginBConfig.class);
applicationContext.refresh();
return applicationContext;
}
}
- 基于父子spring context 解决bean 共享问题
场景,比如bootstrap 提供datasource,插件直接使用此datasource
默认pf4j spring 只能使用自己插件里边包含的bean,对于添加了Extension 注解的扩展类,可以暴露为一个直接spring bean
pf4j-spring/src/main/java/org/pf4j/spring/ExtensionsInjector.java
protected void registerExtension(Class<?> extensionClass) {
Map<String, ?> extensionBeanMap = springPluginManager.getApplicationContext().getBeansOfType(extensionClass);
if (extensionBeanMap.isEmpty()) {
Object extension = springPluginManager.getExtensionFactory().create(extensionClass);
beanFactory.registerSingleton(extensionClass.getName(), extension); // 使用了bootstrap 的applicationcontext
} else {
log.debug("Bean registeration aborted! Extension '{}' already existed as bean!", extensionClass.getName());
}
}
此扩展在bootstrap 的applicationcontext 中注册,对于插件同时也包含一个createApplicationContext,在此处我们可以i基于
spring 的context 父子关系解决插件共享bootstrap bean 的问题
参考代码
public class pluginC extends SpringPlugin {
public pluginC(PluginWrapper wrapper) {
super(wrapper);
}
@Override
protected ApplicationContext createApplicationContext() {
// 先获取SpringPluginManager 的ApplicationContext,也就是bootstrap 的ApplicationContext
ApplicationContext applicationContextRoot = ((SpringPluginManager)getWrapper().getPluginManager()).getApplicationContext();
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.setClassLoader(getWrapper().getPluginClassLoader());
// 设置父applicationtext
applicationContext.setParent(applicationContextRoot);
applicationContext.register(PluginCConfig.class);
applicationContext.refresh();
return applicationContext;
}
}
注意一个问题,将spring 依赖设置为provide,不然会有上边2的问题,注意共享bean是子能访问父,父不能访问子(spring 约定)
如果需要访问,推荐放到服务契约中,在插件中开发实现,让SpringPluginManager 自己进行bean 注册到父中,而且此bean在插件中
也可以直接访问
参考资料
https://github.com/pf4j/pf4j/issues/93
https://github.com/rongfengliang/pf4j-spring-learning/tree/classloader-test
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2021-02-18 集成unpkg npm包发布说明
2021-02-18 unpkg +verdaccio+ webpack 联邦实现多版本控制
2020-02-18 cube.js 支持 elasticsearch
2020-02-18 cube.js schema 学习二
2020-02-18 iperf3+ethr 测试网络工具
2019-02-18 goreleaser 方便的go 二进制包分发工具
2019-02-18 dive 方便的观察容器各层信息的工具