spring 2.5.6 错误:Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] 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
    at org.springframework.context.config.ContextNamespaceHandler$1.parse(ContextNamespaceHandler.java:65)
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:69)
    at 

 

凭感觉,这是jdk版本问题。 我本机安装的是1.8, 对方要求的也是1.8. 他们运行没问题,我这里怎么就不行了呢? 这就奇怪了。。

跟进源码里面看看吧: org.springframework.context.config.ContextNamespaceHandler$1.parse(ContextNamespaceHandler.java:65)

 

private void registerJava5DependentParser(final String elementName, final String parserClassName) {
BeanDefinitionParser parser = null;
if(JdkVersion.isAtLeastJava15()) {
try {
Class parserClass = ClassUtils.forName(parserClassName, ContextNamespaceHandler.class.getClassLoader());
parser = (BeanDefinitionParser)parserClass.newInstance();
} catch (Throwable var5) {
throw new IllegalStateException("Unable to create Java 1.5 dependent parser: " + parserClassName, var5);
}
} else {
parser = new BeanDefinitionParser() {
public BeanDefinition parse(Element element, ParserContext parserContext) {
throw new IllegalStateException("Context namespace element '" + elementName + "' and its parser class [" + parserClassName + "] are only available on JDK 1.5 and higher");
}
};
}

很明显是JdkVersion.isAtLeastJava15() 有蹊跷: 

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

最后找到:

    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 = 1;
        }

    }

原来如此,这个版本的spring(2.5.6) 只能支持到 1.7, 估计当时1.8 没有出来(什么年代了还用2.5.6,真是奇葩啊)。怎么办呢?需要重新安装一个低版本的jdk啊

另外注意到:

private static final String javaVersion = System.getProperty("java.version");

 

还需要,修改 java的 环境变量,不然还是不生效。

 

你可能感兴趣的:(spring 2.5.6 错误:Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher)