繁体   English   中英

Eclipse RCP找不到applicationContext.xml

[英]Eclipse RCP cannot find applicationContext.xml

我正面临着集成Eclipse RCP和Spring IOC的一些问题。

以下是我对该过程的处理方法。

我已经完成的步骤

  1. 创建了一个Bundle(使用来自exisitng archives项目类型的插件),它包含所有Spring jar。
  2. 使用视图创建了一个简单的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的My视图的代码片段部分如下所示

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无法创建视图ID st.view:IOException从类路径资源[applicationContext.xml]解析XML文档; 嵌套异常是java.io.FileNotFoundException:类路径资源[applicationContext.xml]无法打开,因为它不存在!STACK 0 java.io.FileNotFoundException:类路径资源[applicationContext.xml]无法打开

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

我尝试了以下方法。

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

在所有3个案例中,它都说FileNotFoundException 我应该在哪里放置applicationContext.xml文件以使applicationContext引用找到它?

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

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

这个网站被盗

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

你可以在另一个简单的bean中即时通讯吗?

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

然后打电话

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM