我正在使用spring创建一个非常简单的java应用程序。但是应用程序无法打开配置文件(config.xml)。
我正在使用nebeans IDE 8.2 jdk8版本
代码如下:
package com.prasad.spring.springcore;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.*;
import java.util.*;
import java.lang.*;
/**
*
* @author Prasad-pc
*/
public class Test{
public static void main(String args[]){
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("config.xml"); // configuration file name
Employee emp = (Employee) ctx.getBean("emp");
System.out.println("Employee ID: " + emp.getId());
System.out.println("Employee Name: " + emp.getName());
}
}
我得到的错误是:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [config.xml]; nested exception is java.io.FileNotFoundException: class path resource [config.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.prasad.spring.springcore.Test.main(Test.java:21)
Caused by: java.io.FileNotFoundException: class path resource [config.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
... 13 more
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
“ config.xml”位于src / main / java目录中
答案 0 :(得分:0)
config.xml
文件必须位于src/main/resources
目录中,而在上述情况下,文件位于src/main/java
答案 1 :(得分:0)
src/main/java
仅应包含Java类。那里的其他任何东西都将被忽略。
src/main/resources
应该包含您想要包含在当前格式中的任何内容,例如XML文件。