【问题标题】:Eclipse RCP cannot find applicationContext.xmlEclipse RCP 找不到 applicationContext.xml
【发布时间】:2012-09-25 17:16:06
【问题描述】:

我在集成 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 的视图的代码 sn-p 部分如下

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

这会引发错误

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

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

我尝试了以下方法。

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

在所有 3 种情况下,它都显示为 FileNotFoundException。我应该将applicationContext.xml 文件放在哪里以使applicationContext 引用找到它?

【问题讨论】:

  • st.Triangle 是正确的路径吗?

标签: java spring eclipse-plugin eclipse-rcp


【解决方案1】:

您确定 Triangle 在“ctx”类路径中吗?

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

&lt;bean id="str" class="java.lang.String" value="Hello World"&gt;

然后调用

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

【讨论】:

    【解决方案2】:

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

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

    盗自This site

    【讨论】:

      猜你喜欢
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      相关资源
      最近更新 更多