Spring抛出UnsupportedClassVersionError

时光毁灭记忆、已成空白 提交于 2020-01-10 09:19:33

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

一、背景介绍

公司的旧项目今年要微服务化,最近在帮业务部门做demo验证,旧项目用的JDK7,且在JDK8下会出现奇怪的编译问题。而我们新开发的服务是基于JDK8,两个项目之间通过dubbo接口进行调用。

然后今天业务部门的兄弟就找我反映了一个问题,说是项目用JDK7启动会报下边这个错

但是用JDK8就是好的,而JDK8又会有奇怪的编译问题,于是就只能JDK7编译、JDK8启动......希望我们能帮他们解决下。

二、问题原因及解决

暂时还没空写一个demo重现这个问题,先简单记录一下。

问题代码如下所示:

//ClassPathScanningCandidateComponentProvider.java
protected void registerDefaultFilters() {
	this.includeFilters.add(new AnnotationTypeFilter(Component.class));
	ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
	try {
		this.includeFilters.add(new AnnotationTypeFilter(
			((Class<? extends Annotation>) cl.loadClass("javax.annotation.ManagedBean")), false));
		logger.info("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
	}
	catch (ClassNotFoundException ex) {
		// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
	}
	try {
		this.includeFilters.add(new AnnotationTypeFilter(
			((Class<? extends Annotation>) cl.loadClass("javax.inject.Named")), false));
		logger.info("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
	}
	catch (ClassNotFoundException ex) {
		// JSR-330 API not available - simply skip.
	}
}

可以看到这里Spring尝试获取了javax.annotation.ManagedBean这个类,并加了try-catch,但是当引用的jar包中包含了JDK8编译的依赖时,这个报错就变成了UnsupportedClassVersionError。

想解决这个问题,要么就把jar包都换成用JDK7编译的,要么就想办法去掉那些包

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!