java.lang.NoClassDefFoundError: Could not initialize class com.example.demo.BeetlTemplateUtils
jdk11+spring boot 2.4.0,项目打包后运行,报类不存在
看提示,是貌似当前ClassLoader,找不到FastRuntimeEngine
public class GroupTemplate {
/* 模板在运行过程中,class方法,accessory调用等需要的classLoader */
ClassLoader classLoader = Thread.currentThread().getContextClassLoader() != null
? Thread.currentThread().getContextClassLoader()
: GroupTemplate.class.getClassLoader();
你可以在构造GroupTemplate前,先设置Thread.currentThread().setContextClassLoader(xxxx),这里的xxx是能加载到GroupTemplate的
正常情况springboot 不应该这样,你有什么特殊处理么,或者把你的这个工程放到git上,我下载运行看看
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/")
public Object test() {
String msg = "当前时间为:" + LocalDateTime.now();
Map map = new HashMap<>();
map.put("code:", "ok");
map.put("msg", msg);
CompletableFuture.supplyAsync(() -> {
Map map3 = new HashMap();
map3.put("name", "上海");
map3.put("name1", "北京");
map3.put("name2", "广州");
List<String> templates = Arrays.asList("hello,${name}", "world,${name1}", "test,${name2}");
return templates.stream().map(s -> BeetlTemplateUtils.render(s, map3)).collect(Collectors.toList());
}).whenComplete((t, e) -> {
if (e != null) {
e.printStackTrace();
} else {
System.out.println(t);
}
});
return map;
}
}
JDK11,你可以简单打个包出来验证一下
@Slf4j
public class BeetlTemplateUtils {
protected static final GroupTemplate STRING_GROUP_TEMPLATE;
protected static final GroupTemplate CLASS_PATH__GROUP_TEMPLATE;
static {
Configuration cfg = null;
try {
cfg = Configuration.defaultConfiguration();
} catch (IOException e) {
log.error("模板引擎初始化错误:", e);
}
//string template
STRING_GROUP_TEMPLATE = new GroupTemplate(new StringTemplateResourceLoader(), cfg);
STRING_GROUP_TEMPLATE.setErrorHandler(new ReThrowConsoleErrorHandler());
//class path template
CLASS_PATH__GROUP_TEMPLATE = new GroupTemplate(new ClasspathResourceLoader("template/"), cfg);
CLASS_PATH__GROUP_TEMPLATE.setErrorHandler(new ReThrowConsoleErrorHandler());
}
/**
* 渲染模板
*
* @param templateContent
* @param values
* @return
*/
public static String render(String templateContent, Map<String, Object> values) {
Template template = STRING_GROUP_TEMPLATE.getTemplate(templateContent);
values.forEach((k, v) -> template.binding(k, v));
return template.render();
}
/**
* 渲染模板
*
* @param fileName
* @param values
* @return
*/
public static String renderClassPath(String fileName, Map<String, Object> values) {
Template template = CLASS_PATH__GROUP_TEMPLATE.getTemplate(fileName);
values.forEach((k, v) -> template.binding(k, v));
return template.render();
}
}
好的,神奇了
我做了如下修改,可以用,但具体在11下,你的方式为什么不行,我还需要研究
@Configuration
public class TemplateConfig {
public BeetlTemplateUtils beetlTemplateUtils(){
BeetlTemplateUtils templateUtils = new BeetlTemplateUtils();
return templateUtils;
}
}
@Component
public class BeetlTemplateUtils {
protected GroupTemplate STRING_GROUP_TEMPLATE;
protected GroupTemplate CLASS_PATH__GROUP_TEMPLATE;
public BeetlTemplateUtils() {
Configuration cfg = null;
try {
cfg = Configuration.defaultConfiguration();
} catch (IOException e) {
e.printStackTrace();
System.out.println("模板引擎初始化错误:");
throw new RuntimeException(e);
}
//string template
STRING_GROUP_TEMPLATE = new GroupTemplate(new StringTemplateResourceLoader(), cfg);
STRING_GROUP_TEMPLATE.setErrorHandler(new ReThrowConsoleErrorHandler());
//class path template
CLASS_PATH__GROUP_TEMPLATE = new GroupTemplate(new ClasspathResourceLoader("template/"), cfg);
CLASS_PATH__GROUP_TEMPLATE.setErrorHandler(new ReThrowConsoleErrorHandler());
}
}
//忽略其他方法
这样,你的controller按照spring方式写就好了,
@Autowired
BeetlTemplateUtils beetlTemplateUtils;
@GetMapping("/")
public Object test() {
}
登录 后才可以发表评论