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-context 2.5.6下的包只支持JDK1.5,JDK1.6和JDK1.7
package org.springframework.context.config;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.JdkVersion;
import org.springframework.util.ClassUtils;
import org.w3c.dom.Element;
public class ContextNamespaceHandler extends NamespaceHandlerSupport {
public ContextNamespaceHandler() {
}
public void init() {
this.registerBeanDefinitionParser("property-placeholder", new PropertyPlaceholderBeanDefinitionParser());
this.registerBeanDefinitionParser("property-override", new PropertyOverrideBeanDefinitionParser());
this.registerJava5DependentParser("annotation-config", "org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser");
this.registerJava5DependentParser("component-scan", "org.springframework.context.annotation.ComponentScanBeanDefinitionParser");
this.registerBeanDefinitionParser("load-time-weaver", new LoadTimeWeaverBeanDefinitionParser());
this.registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
this.registerBeanDefinitionParser("mbean-export", new MBeanExportBeanDefinitionParser());
this.registerBeanDefinitionParser("mbean-server", new MBeanServerBeanDefinitionParser());
}
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");
}
};
}
this.registerBeanDefinitionParser(elementName, parser);
}
}
在**ContextNamespaceHandler**这个类**registerJava5DependentParser**方法中我们可以很明显看到报错的来源:
最后我们,找到头部的if判断,进入调试看看。
```bash
package org.springframework.core;
public abstract 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 JdkVersion() {
}
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 = 1;
}
}
}
也就是说,只支持JDK1.5,JDK1,6和JDK1.7的判断。并不支持JDK1.8。所以可能是此代码写的时候还没有JDK1.8。故可以将项目使用的JDK改为其他三种即可。