3

我在集成 Eclipse RCP 和 Spring IOC 时遇到了一些问题。

以下是我的处理方法。

我已经完成的步骤

  1. 创建了一个包含所有 Spring jar 的 Bundle(使用现有档案项目类型的插件)。
  2. 创建了一个带有视图的 Simple Hello RCP 应用程序。
  3. 将 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 的视图的代码片段部分如下

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:/applicationContext.xml");
Triangle triangle = (Triangle) applicationContext.getBean("JGID");
triangle.draw();

这会引发错误

找不到文件 [D:\applicationContext.xml] 中定义的名称为“JGID”的 bean 的类 [st.Triangle];嵌套异常是 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]; 嵌套异常是 java.io.FileNotFoundException:无法打开类路径资源 [applicationContext.xml],因为它不存在!堆栈 0 java.io.FileNotFoundException:无法打开类路径资源 [applicationContext.xml]

我的 Eclipse RCP 应用程序中的上述代码行,它在哪里检查或查找 xml 文件。

我尝试了以下方法。

  • 将 applicationContext.xml 放在 src 文件夹中。
  • 在项目文件夹中。
  • 在包装内。

在所有 3 种情况下,它都说FileNotFoundException. 我应该在哪里放置applicationContext.xml文件以进行applicationContext参考以找到它?

4

2 回答 2

1

spring-context.xml放在应用程序的类路径中,为了调用它,您需要以下代码

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-context.xml");
            Triangle triangle = (Triangle) ctx.getBean("jgid");
             triangle.draw();  

从本站盗取

于 2012-10-04T07:38:45.387 回答
1

你确定三角形在“ctx”类路径中吗?

例如,您可以实例化另一个简单的 bean

<bean id="str" class="java.lang.String" value="Hello World">

然后打电话

String str= (String) ctx.getBean("str");

于 2012-10-04T07:55:56.953 回答