编辑:This问题的最佳答案是关于不同的编译和生产环境。第二个答案是关于在Eclipse中修复它。我在IntelliJ中运行此代码,并且我没有不同的编译和生产环境。我正在点击IntelliJ中的Run按钮。此外,问题无处不在于那里的问题。
我正在尝试构建一个简单的Spring程序。这是文件夹结构:
.
├── firstSpringProject.iml
├── pom.xml
├── src
│ └── main
│ ├── java
│ │ └── com
│ │ └── webograffiti
│ │ ├── MainClass.java
│ │ └── Student.java
│ └── resources
│ └── applicationContext.xml
以下是用这些文件编写的代码:
Student.java
package com.webograffiti;
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void displayInfo(){
System.out.println("Hello "+name);
}
}
MainClass.java
package com.webograffiti;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {
public static void main(String [] args){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student obj = (Student) context.getBean("student");
obj.displayInfo();
}
}
applicationContext.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-3.0.xsd">
<bean id = "student" class = "com.webograffiti.Student">
<property name = "name" value = "Piyush"/>
</bean>
</beans>
我正在尝试运行MainClass.main()
。但得到这个例外:
Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.beans.support.ResourceEditorRegistrar.<init>(Lorg/springframework/core/io/ResourceLoader;)V
at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:446)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:355)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.webograffiti.MainClass.main(MainClass.java:8)
我拥有运行此代码所需的所有依赖项,即。 spring-core, spring-beans, spring-osgi-core
。这可能是类路径错误,但我无法找到解决方案。