我正在尝试创建我的第一个Spring应用程序。它显示以下错误:
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();
}
XML文件位于src
文件夹中。
答案 0 :(得分:1)
代替:
Resource resource = new ClassPathResource("applicationContext");
使用:
Resource resource = new ClassPathResource("applicationContext.xml");