2

I am trying to create my first Spring app. It is showing the following error:

log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
    at com.spring1.FirstSpring1.main(FirstSpring1.java:20)
Caused by: java.io.FileNotFoundException: class path resource [applicationContext] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    ... 4 more

GetterSetter.java

package com.spring1;

private String name;

public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name=name;
}

public void displayInfo()
{
    System.out.println("hello "+name);
}

FirstSpring1.java

package com.spring1;

public static void main(String[] args)
{
    GetterSetter gt=new GetterSetter();
    gt.setName("Google");
    gt.displayInfo();

    Resource resource = new ClassPathResource("applicationContext");
    BeanFactory factory = new XmlBeanFactory(resource);
    GetterSetter gt1 = (GetterSetter)factory.getBean("name1");
    gt1.displayInfo();
}

The XML file is in the src folder.

4
  • I edited the title to reflect the actual exception (the exception reported in the original title was simply LOG4J complaining about itself). Commented Mar 3, 2019 at 12:55
  • You should use applicationContext.xml instead of applicationContext. This is what exception says: FileNotFoundException: class path resource [applicationContext] cannot be opened because it does not exist. Commented Mar 3, 2019 at 16:17
  • 1
    it is working thank you @MichalZiober i replaced applicationContext with applicationContext.xml Commented Mar 4, 2019 at 14:29
  • Great, I have added my comment as answer, so you can accept it. Commented Mar 4, 2019 at 14:47

1 Answer 1

1

Instead of:

Resource resource = new ClassPathResource("applicationContext");

Use:

Resource resource = new ClassPathResource("applicationContext.xml");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.