1
votes

enter image description here

I'm learning spring dependency injection. I have 2 types of code. One works and one doesn't... But, they both work for the person who made the tutorial.

The commented code gives me the error shown below.

    @SuppressWarnings("deprecation")
public static void main(String[] args) {

    //ApplicationContext factory = new ClassPathXmlApplicationContext("Beans.xml");

    //The code below works
    BeanFactory factory = new XmlBeanFactory(new FileSystemResource("Beans.xml"));
    HelloWorld obj = (HelloWorld) factory.getBean("helloworld");

    obj.getMessage();
}

Beans.xml

 <?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="
           http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd           
       ">


    <bean id="helloworld" class="com.vulab.hellow.HelloWorld">
        <property name="message" value="Hello World" />
    </bean>
</beans>

Error message when I use ApplicationContext

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [Beans.xml] cannot be opened because it does not exist

2
file Beans.xml or file beans.xml ? check the case ? - Jerome Cance
Its the same in the IDE. @JeromeC. - AppSensei
Where did you place your Beans.xml file? Is it inside a source folder (does it get copied to your classes folder whenever you compile your app)? - Claudio
@ClaudioFernandez How do I make sure of that? I opened my classes folder..didnt see anything called Beans.xml - AppSensei
put your Beans.xml into the same directory of the class which call it. If you use maven, you can put it in the resource folder. - Jerome Cance

2 Answers

2
votes

For XmlBeanFactory to work, Beans.xml would have to be in the same directory as the calling class.

An easy alternative is make sure that Beans.xml is located in your classpath. You could copy them into src\resources and then use:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) applicationContext.getBean("helloworld");

ClassPathXmlApplicationContext is more convenient as absolute file locations do not need to be specified.

Note: As of Spring 3.1 XmlBeanFactory is a deprecated which means that an alternative such as this should be used should you change from 3.0.

1
votes

For the ClassPathXmlApplicationContext, the Application Context XML file will need to be available in your ClassPath.

If you are using standard Maven directory layout, you will want to place your Beans.xml file in src/main/resources.

If you are running from your IDE (it looks like you are using Eclipse from your screenshot, though you did not explicitly state your IDE), you will want to go into your Properties->Java Build Path and add src/main/resources to the build path.

Good luck, hope this helps!