我在集成 Eclipse RCP 和 Spring IOC 时遇到了一些问题。
以下是我的处理方法。
我已经完成的步骤
- 创建了一个包含所有 Spring jar 的 Bundle(使用来自现有文件项目类型的插件)。
- 创建了一个带有 View 的简单 Hello RCP 应用程序。
- 将 Bundle 添加为 RCP 项目的依赖项(第 2 步)
在我的 RCP 项目中创建了一个简单的类,其对象必须通过 applicationContext.xml 实例化。
public class Triangle {
public void draw(){
System.out.println("Traingle drawn");
}
}
我的applicationContext.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="JGID" class="st.Triangle"/>
</beans>
我正在获取 applicationContext.xml 的 View 的代码片段部分如下所示
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:/applicationContext.xml");
Triangle triangle = (Triangle) applicationContext.getBean("JGID");
triangle.draw();
这会抛出错误
Cannot find class [st.Triangle] for bean with name 'JGID' defined in file [D:\applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: st.Triangle
如何解决这个错误?
作为解决方法,我尝试了另一种方法,但我也失败了,如下所示,即使用 ClassPathXmlApplicationContext
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
错误如下
!MESSAGE Unable to create view ID st.view: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist !STACK 0 java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened
我的 Eclipse RCP 应用程序中的上述代码行,它在哪里检查或查找 xml 文件。
我尝试了以下方法。
- 将 applicationContext.xml 放在 src 文件夹中。
- 在项目文件夹中。
- 在包中。
在所有 3 种情况下,它都显示 FileNotFoundException
。我应该将 applicationContext.xml
文件放在哪里,以便让 applicationContext
引用找到它?
最佳答案
您确定 Triangle 在“ctx”类路径中吗?
例如,你能实例化另一个简单的 bean吗
<bean id="str" class="java.lang.String" value="Hello World">
然后调用
String str= (String) ctx.getBean("str");
关于java - Eclipse RCP 找不到 applicationContext.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12587908/