简体   繁体   中英

Eclipse RCP cannot find applicationContext.xml

I am facing some issues integrating Eclipse RCP and Spring IOC.

Below is my approach to the process.

Steps I've done

  1. Created a Bundle (Using plug-in from exisitng archives project type) which has all Spring jars.
  2. Created a Simple Hello RCP application with a view.
  3. Added the Bundle as a dependency to the RCP project(step2)

Created a simple class in my RCP project whose object has to instantiated through applicationContext.xml.

public class Triangle {
            public void draw(){
                System.out.println("Traingle drawn");
            }
       }

My applicationContext.xml code

<?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>

A code snippet part of My view where I'm fetching applicationContext.xml is as below

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

this throws error

Cannot find class [st.Triangle] for bean with name 'JGID' defined in file [D:\\applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: st.Triangle

How do I resolve this error ?

As a workaround I tried the other way, also I failed as in below ie., using ClassPathXmlApplicationContext

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

Below is the error

!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

The above code line in my Eclipse RCP applcication, where does it check or look for the xml file.

I tried the following ways.

  • Placed applicationContext.xml in src folder.
  • At the project folder.
  • Inisde the package.

In all the 3 cases, it says FileNotFoundException . Where should I place the applicationContext.xml file to make the applicationContext reference to find it?

Place the spring-context.xml in the class path of your application and in order to invoke it, you need the following code

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

Stolen from This site

are you sure that Triangle is in "ctx" classpath?

can you instantinate another simple bean for example

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

and then call

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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