beetl模板使用场景_Configuration中的ClassLoader加载类异常

本文介绍了在使用Beetl 3.3.1.RELEASE时,遇到因自定义ClassLoader导致的NoClassDefFoundError异常。问题出现在尝试加载beetl-default.properties文件并初始化TAG.includeJSP时,由于包含了不需要的类 javax.servlet.http.HttpServletRequest。解决方案是扩展Configuration类,增加一个带ClassLoader参数的构造函数,并使用系统类加载器,避免加载不必要的类。
摘要由CSDN通过智能技术生成

Version: 3.3.1.RELEASE

场景: 自定义了一个ClassLoader,并设置了线程上下文ClassLoader为自定义的ClassLoader,然后使用Beetl渲染模板,报Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest。

伪代码:

Maven:

cn.hutool

hutool-all

5.5.2

com.ibeetl

beetl

3.3.1.RELEASE

Class:

public class BeetlTest {

public static void main(String[] args) {

File file = FileUtil.file(

"C:\\Users\\admin\\.m2\\repository\\org\\apache\\tomcat\\embed\\tomcat-embed-core\\9.0.41\\tomcat-embed-core-9.0.41.jar");

JarClassLoader jarClassLoader = JarClassLoader.loadJar(file);

Thread.currentThread().setContextClassLoader(jarClassLoader);

// 1. do some business must use JarClassLoader

// business code......

// 2. render beetl template

executeBeetl();

}

public static void executeBeetl(){

try {

// 这里出错的。

Configuration cfg = Configuration.defaultConfiguration();

GroupTemplate groupTemplate = new GroupTemplate(new StringTemplateResourceLoader(), cfg);

Template template = groupTemplate.getTemplate("Print name=AAAAA");

System.out.println(template.render());

} catch (IOException e) {

e.printStackTrace();

}

}

}

简单分析:

执行Configuration.defaultConfiguration();时,不能指定类加载器,默认使用线程上下文类加载器了。

而因为加载了tomcat的包,因此根据Beetl逻辑加载beetl-default.properties时,会添加下面的TAG:

TAG.incdlueJSP= org.beetl.ext.jsp.IncludeJSPTag,javax.servlet.http.HttpServletRequest

加载org.beetl.ext.jsp.IncludeJSPTag类时,就出错了。

正常情况下,不会加载TAG.incdlueJSP的,实际上这里的业务也不需要。

个人拙见:

Configuration类添加一个可以指定ClassLoader的构造函数,使用时用ClassLoader.getSystemClassLoader()即可。

系统类加载器不包含HttpServletRequest,于是这样就不会初始化这个TAG了:TAG.incdlueJSP= org.beetl.ext.jsp.IncludeJSPTag,javax.servlet.http.HttpServletRequest。问题也就不会发生了。

public Configuration(ClassLoader classloader) {

this.classloader = classloader;

initDefault();

}

使用:

Configuration cfg = new Configuration(ClassLoader.getSystemClassLoader());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值