我是 Spring 新手,我正在尝试使用 Spring 容器构造对象,该容器将解析我的 XML 文件,所以我有一个
.xml
和两个 .java
类。
春季版本5.1.0
我正在寻找类似的解决方案,但有些是 Maven 解决方案,不适用于 Spring。
employeeBeanFile.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="emp" class="co.bin.rito.Employee">
<property name="eid" value="143529" />
<property name="ename" value="Rito Sarkar" />
<property name="eaddress" value="Tower Two Magarpatta, S5-458" />
</bean>
Employee.java
public class Employee {
// this contains attributes
// constructor and methods
// and set, get and toString() function
}
客户端.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
// Spring way | IOC (Inversion of control)
ApplicationContext context = new ClassPathXmlApplicationContext("employeeBeanFile.xml");
Employee e = (Employee) context.getBean("emp");
System.out.println("Employee one details: " + e);
((AbstractApplicationContext) context).close();
}}
使用此代码我遇到以下错误
Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.clearCache()V
at org.springframework.context.support.AbstractApplicationContext.resetCommonCaches(AbstractApplicationContext.java:910)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:573)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at co.bin.rito.Client.main(Client.java:11)
出现此错误是由于 Spring 中不同版本的 jar 造成的。请在您的 pom 中使用正确的 jar 依赖项。还要确保您的构建路径中不包含其他版本的 jars。