解决旧版本Spring(2.5.5)无法在新版JDK(1.8以上)上启动的问题

2021-06-27 1649点热度 0人点赞 0条评论

老系统使用的是Spring2.5.5,准备把老系统换到新的服务器上,安装好了新版的JDK11,却发现无法启动tomcat,查看tomcat/logs/catalina.out的日志:

Unexpected exception parsing XML document from class path resource [applicationContext-dao.xml]; nested exception is java.lang.IllegalStateException: Context namespace element 'annotation-config' and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on JDK 1.5 and higher
Caused by: java.lang.IllegalStateException: Context namespace element 'annotation-config' and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on JDK 1.5 and higher

发现Spring居然监测到的JDK版本是1.5以下。

如果要换Spring的话,老系统不知道要改写什么东西,工程量比较大,找到网上一个解决方案(是解决1.8的JDK的,我稍加修改了下,解决1.8以上全部的JDK),总体思路是修改Spring判断JDK版本的类,重新编译并覆盖掉原来的。

在项目中,创建org.springframework.core这个package,然后新建一个类:JdkVersion.java,内容如下:

/*
 * Copyright (c) 2021.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.springframework.core;

/**
 * @author Terry E-mail: yaoxinghuo at 126 dot com
 * @date 2021-6-25 14:19
 * @description
 */
public class JdkVersion {
    public static final int JAVA_13 = 0;
    public static final int JAVA_14 = 1;
    public static final int JAVA_15 = 2;
    public static final int JAVA_16 = 3;
    public static final int JAVA_17 = 4;
    private static final String javaVersion = System.getProperty("java.version");
    private static final int majorJavaVersion;

    public static String getJavaVersion() {
        return javaVersion;
    }

    public static int getMajorJavaVersion() {
        return majorJavaVersion;
    }

    public static boolean isAtLeastJava14() {
        return true;
    }

    public static boolean isAtLeastJava15() {
        return getMajorJavaVersion() >= 2;
    }

    public static boolean isAtLeastJava16() {
        return getMajorJavaVersion() >= 3;
    }

    static {
        if (javaVersion.indexOf("1.7.") != -1) {
            majorJavaVersion = 4;
        } else if (javaVersion.indexOf("1.6.") != -1) {
            majorJavaVersion = 3;
        } else if (javaVersion.indexOf("1.5.") != -1) {
            majorJavaVersion = 2;
        } else {
            majorJavaVersion = 5; //修改这里,监测JDK,都当成高版本1.8以上,相信也不可能找到再旧版的JDK了!
        }
    }
}

重新编译了一下,在 build/classes/org/springframework/core/JdkVersion.class 这里找到JdkVersion.class,然后用解压缩软件打开项目的lib目录里的spring-2.5.5.jar,把里面的org/springframework/core里的JdkVersion.class替换成我们编译出来的,再放回原来的lib下,就可以顺利启动Tomcat啦!

如果你懒得编译,我已经把编译修改好的spring.2.5.5.jar传到github上了,供有需要的下载:

https://github.com/yaoxinghuo/githubprojects/blob/master/springtest/WebContent/WEB-INF/lib/spring-2.5.5.jar?raw=true

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论