0
votes

I just started to learn Spring Framework, installed Eclipse and added Spring and Maven. I made my first program, of course, Hello World but using spring beans. Everything was "by the book", I made some class, let's say Person, and main class that will instantiate it through beans in xml spring beans configuration file. I made beans.xml file right in my project just like in book example, and wrote following code:

package maven.aplikacije.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App 
{
 public static void main( String[] args )
 {
    ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
    Person helloBean = (Person)ac.getBean("person");
    helloBean.sayHello();
 }
 }

This is a beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "person" class = "maven.aplikacije.Person.Person"></bean>

</beans>

However, it does not work. It lists following error (among others):

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

I tried to write

ApplicationContext ac = new ClassPathXmlApplicationContext("Person//beans.xml");

and plenty more different versions but nothing works. Of course, I also defined Person class. :)

Why doesn't program "see" beans.xml file?

Thank you in advance!

1
Could you share the file layout of your project? - Mark Elliot
If you are starting to learn spring for heavens sake don't use XML config! Learn annotations and JavaConfig. XML config is essentially deprecated these days; there is absolutely no point in learning outdated technologies. - Boris the Spider
Further; please use Java naming conventions. PascalCase is reserved for class names, please use lower case for package names. - Boris the Spider
I just started so I didn't know. Thank you all for useful advice. You are right about both things. I just didn't still found some tutorial on annotations and JavaConfig and naming package should be in lower case. - Biljana M.

1 Answers

1
votes

when you refer it from classpath at Person/beans.xml,

ApplicationContext ac = new ClassPathXmlApplicationContext("Person//beans.xml");

you would need to place beans.xml at following location in your maven project

src/main/resources/Person/beans.xml

if you try to read

ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

then

src/main/resources/beans.xml

or you can put it anywhere and configure maven to place beans.xml file in your classpath with specified namespace