【发布时间】:2016-07-28 10:11:37
【问题描述】:
我正在使用 Jrebel 6.3.0 为我的 Spring Boot Web 应用程序热重载 Mybatis 的 Mapper XML。并且我使用Java Configuration来配置Mybatis。
@Configuration
@ConditionalOnClass({ PageInterceptor.class })
@EnableConfigurationProperties(MybatisPageProperties.class)
@AutoConfigureBefore(MybatisAutoConfiguration.class)
public class MyBatisPageAutoConfiguration implements ApplicationContextAware {
private static final Logger LOG = LoggerFactory.getLogger(MyBatisPageAutoConfiguration.class);
@Autowired
private MybatisPageProperties properties;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
BeanUtil.setApplicationContext(applicationContext);
}
/**
*
*
* @return
*/
@Bean
public PageInterceptor pageInterceptor() {
LOG.info("========PageInterceptor========");
PageInterceptor pageInterceptor = new PageInterceptor();
Properties p = new Properties();
p.setProperty("dialect", properties.getDialect());
p.setProperty("sqlIdRegex", properties.getSqlIdRegex());
pageInterceptor.setProperties(p);
return pageInterceptor;
}
我发现 Jrebel 在重新加载映射器 xml 文件时清除了拦截器。
org.zeroturnaround.jrebel.mybatis.SqlMapReloader
private void reconfigure() {
log.infoEcho("Reloading SQL maps");
this.conf.reinit(); // Clear loadedResources and interceptors
reloadedResources.set(Collections.synchronizedSet(new HashSet()));
enterReloading();
try {
if (this.confBuilder != null)
this.confBuilder.reinit();
reconfigureAdditionalMappings();
exitReloading();
reloadedResources.remove();
}
finally
{
exitReloading();
reloadedResources.remove();
}
}
.........
private void reconfigureAdditionalMappings() {
for (ResourceDesc rd : (ResourceDesc[])this.additionalMappings.toArray(new ResourceDesc[0])) {
reconfigureMapping(rd);
}
}
private void reconfigureMapping(ResourceDesc rd) {
org.apache.ibatis.session.Configuration c = (org.apache.ibatis.session.Configuration)this.conf;
try { // Only reload loadedResources from Mapper XML files.
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(ResourceUtil.asInputStream(rd.url), c, rd.path, c.getSqlFragments());
xmlMapperBuilder.parse();
}
catch (Exception e) {
if ((e.getCause() instanceof java.io.FileNotFoundException)) removeMappingForDeletedResource(rd); else {
throw new RuntimeException("Failed to parse mapping resource: '" + rd.url + "'", e);
}
} finally {
ErrorContext.instance().reset();
}
}
org.zeroturnaround.jrebel.mybatis.cbp.ConfigurationCBP
public class ConfigurationCBP
extends JavassistClassBytecodeProcessor
{
public void process(ClassPool cp, ClassLoader cl, CtClass ctClass)
throws Exception
{
ctClass.addInterface(cp.get(JrConfiguration.class.getName()));
ctClass.addField(new CtField(cp.get(SqlMapReloader.class.getName()), "reloader", ctClass));
CtConstructor[] constructors = ctClass.getConstructors();
for (int i = 0; i < constructors.length; i++) {
CtConstructor constructor = constructors[i];
if (constructor.callsSuper()) {
constructor.insertAfter("reloader = new " + SqlMapReloader.class.getName() + "($0);");
}
}
ctClass.addMethod(CtNewMethod.make("public " + SqlMapReloader.class
.getName() + " getReloader() {" + " return reloader;" + "}", ctClass));
ctClass.addMethod(CtNewMethod.make("public void reinit() { loadedResources.clear(); ((" + JrInterceptorChain.class
.getName() + ") interceptorChain).jrClear();" + "}", ctClass));
ctClass.getDeclaredMethod("isResourceLoaded").insertAfter("if (reloader.doReload($1)) { loadedResources.remove($1); $_ = false;}");
}
}
有没有办法让 Jrebel 保留拦截器?
【问题讨论】:
-
您找到解决问题的方法了吗?
-
我没有修改jrebel,只修改了mybatis中的一个类。 org.apache.ibatis.builder.xml.XMLMapperBuilder。在方法的末尾 public void parse() { 添加以下代码: // 由元进用添加 if (interceptors.size() == 0) { interceptors.addAll(configuration.getInterceptors()); } if (configuration.getInterceptors().size() == 0) { for (Interceptor 拦截器: 拦截器) { configuration.addInterceptor(interceptor); } }
-
你修改了类本身吗?或者也许有一种方法可以在你的代码中创建一个扩展 XMLMapperBuilder 的新类,并以某种方式将 mybatis 使用的类替换为你编写的类?
-
我的项目是web app,我们不需要在prod-env中做这个改变,我们只是在dev-env中使用jrebel,所以只需修改从源jar中获取的源文件,并将它在 src/test/java 中,它将被构建到类目录,并替换原始 jar 中的类。不要放在 src/main/java 文件夹中。