Spring - <constructor-arg> - java.lang.IncompatibleClassChangeError
Spring - <constructor-arg> - java.lang.IncompatibleClassChangeError
您好,我使用 EclipseEE 和 Java8_40。我从 Spring 框架开始,但遇到了一些问题。当我想在 beans.xml 中使用构造函数时,程序会创建一个异常。
我在 CLASSPATH 中:
- commons-logging-1.2.jar
- commons-logging-1.2-javadoc.jar
和 Spring *.jars
- spring-aop-4.1.5.RELEASE.jar
- spring-asm-3.1.1.RELEASE.jar
- spring-beans-4.1.5.RELEASE.jar
- spring-context-4.1.5.RELEASE.jar
- spring-方面-4.1.5.RELEASE.jar
- spring-context-support-4.1.5.RELEASE.jar
- spring-core-4.1.5.RELEASE.jar
- spring-web-4.1.5.RELEASE.jar
- spring-webmvc-4.1.5.RELEASE.jar
- spring-expression-4.1.5.RELEASE.jar
SPRING豆子
package beans;
public class SequenceGenerator {
private String prefix;
private String suffix;
private int initial;
private int counter;
public SequenceGenerator() {}
public SequenceGenerator(String prefix, String suffix, int initial) {
this.prefix = prefix;
this.suffix = suffix;
this.initial = initial; }
public void setPrefix(String prefix) {
this.prefix = prefix; }
public void setSuffix(String suffix) {
this.suffix = suffix; }
public void setInitial(int initial) {
this.initial = initial; }
public synchronized String getSequence() {
StringBuffer buffer = new StringBuffer();
buffer.append(prefix);
buffer.append(initial + counter++);
buffer.append(suffix);
return buffer.toString(); }
}
客户
package klient.program;
import org.springframework.context.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.SequenceGenerator;
public class Program {
@SuppressWarnings("resource")
public static void main(String [] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
SequenceGenerator generator = (SequenceGenerator) context.getBean("sequenceGenerator");
System.out.println(generator.getSequence());
System.out.println(generator.getSequence());
}
}
BEANS.XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<bean name="sequenceGenerator" class="beans.SequenceGenerator">
<constructor-arg><value>30</value></constructor-arg>
<constructor-arg><value>A</value></constructor-arg>
<constructor-arg><value>100000</value></constructor-arg>
</bean>
</beans>
异常:
Exception in thread "main" java.lang.IncompatibleClassChangeError: class org.springframework.core.LocalVariableTableParameterNameDiscoverer$ParameterNameDiscoveringVisitor has interface org.springframework.asm.ClassVisitor as super class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access0(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.core.LocalVariableTableParameterNameDiscoverer.inspectClass(LocalVariableTableParameterNameDiscoverer.java:114)
at org.springframework.core.LocalVariableTableParameterNameDiscoverer.getParameterNames(LocalVariableTableParameterNameDiscoverer.java:87)
at org.springframework.core.PrioritizedParameterNameDiscoverer.getParameterNames(PrioritizedParameterNameDiscoverer.java:65)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:182)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1131)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1034)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at klient.program.Program.main(Program.java:8)
您不需要 spring-asm-(version).jar。
此依赖项早于 spring-core:
Note however that we’ve eliminated the dedicated spring-asm jar in M2
in favor of including org.springframework.asm classes directly in
spring-core. Both of these upgrades are good news for JDK7 users
writing Spring components in dynamic JVM languages, as these new
versions of CGLIB and ASM properly handle the new invokedynamic
bytecode instruction introduced in JDK7.
http://spring.io/blog/2012/09/12/spring-framework-3-2-m2-released
这里是怎么写的:https://github.com/spring-projects/spring-framework/commit/c16f18a5fd659f34d6290b5f2cffe54534b7592a 这个模块完全删除,而是使用 ASM 4
您好,我使用 EclipseEE 和 Java8_40。我从 Spring 框架开始,但遇到了一些问题。当我想在 beans.xml 中使用构造函数时,程序会创建一个异常。
我在 CLASSPATH 中:
- commons-logging-1.2.jar
- commons-logging-1.2-javadoc.jar
和 Spring *.jars
- spring-aop-4.1.5.RELEASE.jar
- spring-asm-3.1.1.RELEASE.jar
- spring-beans-4.1.5.RELEASE.jar
- spring-context-4.1.5.RELEASE.jar
- spring-方面-4.1.5.RELEASE.jar
- spring-context-support-4.1.5.RELEASE.jar
- spring-core-4.1.5.RELEASE.jar
- spring-web-4.1.5.RELEASE.jar
- spring-webmvc-4.1.5.RELEASE.jar
- spring-expression-4.1.5.RELEASE.jar
SPRING豆子
package beans;
public class SequenceGenerator {
private String prefix;
private String suffix;
private int initial;
private int counter;
public SequenceGenerator() {}
public SequenceGenerator(String prefix, String suffix, int initial) {
this.prefix = prefix;
this.suffix = suffix;
this.initial = initial; }
public void setPrefix(String prefix) {
this.prefix = prefix; }
public void setSuffix(String suffix) {
this.suffix = suffix; }
public void setInitial(int initial) {
this.initial = initial; }
public synchronized String getSequence() {
StringBuffer buffer = new StringBuffer();
buffer.append(prefix);
buffer.append(initial + counter++);
buffer.append(suffix);
return buffer.toString(); }
}
客户
package klient.program;
import org.springframework.context.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.SequenceGenerator;
public class Program {
@SuppressWarnings("resource")
public static void main(String [] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
SequenceGenerator generator = (SequenceGenerator) context.getBean("sequenceGenerator");
System.out.println(generator.getSequence());
System.out.println(generator.getSequence());
}
}
BEANS.XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<bean name="sequenceGenerator" class="beans.SequenceGenerator">
<constructor-arg><value>30</value></constructor-arg>
<constructor-arg><value>A</value></constructor-arg>
<constructor-arg><value>100000</value></constructor-arg>
</bean>
</beans>
异常:
Exception in thread "main" java.lang.IncompatibleClassChangeError: class org.springframework.core.LocalVariableTableParameterNameDiscoverer$ParameterNameDiscoveringVisitor has interface org.springframework.asm.ClassVisitor as super class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access0(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.core.LocalVariableTableParameterNameDiscoverer.inspectClass(LocalVariableTableParameterNameDiscoverer.java:114)
at org.springframework.core.LocalVariableTableParameterNameDiscoverer.getParameterNames(LocalVariableTableParameterNameDiscoverer.java:87)
at org.springframework.core.PrioritizedParameterNameDiscoverer.getParameterNames(PrioritizedParameterNameDiscoverer.java:65)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:182)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1131)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1034)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at klient.program.Program.main(Program.java:8)
您不需要 spring-asm-(version).jar。 此依赖项早于 spring-core:
Note however that we’ve eliminated the dedicated spring-asm jar in M2 in favor of including org.springframework.asm classes directly in spring-core. Both of these upgrades are good news for JDK7 users writing Spring components in dynamic JVM languages, as these new versions of CGLIB and ASM properly handle the new invokedynamic bytecode instruction introduced in JDK7.
http://spring.io/blog/2012/09/12/spring-framework-3-2-m2-released
这里是怎么写的:https://github.com/spring-projects/spring-framework/commit/c16f18a5fd659f34d6290b5f2cffe54534b7592a 这个模块完全删除,而是使用 ASM 4