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 ,可以声明我们需要的bea
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
到此这篇关于pf4j spring 集成几个问题的文章就介绍到这了,更多相关pf4j spring 集成内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
全网最新springboot整合mybatis-plus的过程
在本文中,介绍了 MyBatis-Plus 的核心功能和使用方法,包括如何配置分页插件、编写分页查询代码、使用各种 Wrapper 构建复杂查询条件等,通过这些内容,相信你已经对 MyBatis-Plus 有了更深入的了解,并能够在实际项目中灵活应用这些功能,感兴趣的朋友跟随小编一起看看吧2025-02-02
MyBatis typeAliases元素标签(含注解方式)及其属性、设置方式
这篇文章主要介绍了MyBatis typeAliases元素标签(含注解方式)及其属性、设置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-09-09


最新评论