Reputation: 5136
I am facing some issues integrating Eclipse RCP and Spring IOC.
Below is my approach to the process.
Steps I've done
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 i.e.., 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.
In all the 3 cases, it says FileNotFoundException
. Where should I place the applicationContext.xml
file to make the applicationContext
reference to find it?
Upvotes: 3
Views: 1385
Reputation: 1559
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
Upvotes: 1
Reputation: 1466
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");
Upvotes: 1