Spring出现only available on JDK 1.5 and higher错误详细解决方案!!!

Spring出现only available on JDK 1.5 and higher错误详细解决方案!!!

  • 概述:

概述:

之前在学习Spring的时候出现了与jdk版本不兼容的问题,在运行时就会报错:

Caused by: java.lang.IllegalStateException: Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

在查看了JdkVersion类的源码后发现Spring只能识别到jdk1.7,对于更高的版本则无法进行识别,解决问题有两种解决办法:
1.直接更换JDK为jdk1.7或者更低版本,但是可能会对其他的一些jar包不支持(反正我是出现了这种情况)

2.第二种手动修改spring的jar包,在org.springframework.core目录下,有一个JdkVersion.class,自己参照包路径,重新写一个JdkVersion.java。(推荐)

修改后的JdkVersion.java文件如下:

package org.springframework.core;
 
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;
    
    //还可以参照该模板更改对更高版本JDK的支持
    //for jre 1.8
    public static final int JAVA_18 = 5;
    
    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
    {
        //for jre 1.8
        if (javaVersion.indexOf("1.8.") != -1)
        {
            majorJavaVersion = 5;
        }
        else 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 = 1;
        }
    }
}

接下来来看一下具体修改Jar包中的class源码的具体步骤,参考我的另一篇博客:
https://blog.csdn.net/HuanglnQuan/article/details/89205223

你可能感兴趣的:(Spring)