Spring / NetBeans - java.io.FileNotFoundException:类路径资源[beans.xml]无法打开,因为它不存在

时间:2017-03-21 03:19:39

标签: java xml spring netbeans

每当我尝试运行我的java项目时,我都会发现找不到文件的异常,说找不到beans.xml。我正在使用NetBeans并且已经读过我可能需要将我的类路径设置为正确的目录,否则项目将无法正常运行(Eclipse会自动执行此操作(?))。使用相同的库运行相同的程序与Eclipse一起使用。我不知道我的问题是与Spring相关还是与类路径相关(但我认为它是类路径问题)。

MainApp.java

package hello;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        HelloWorld hello = (HelloWorld) context.getBean("helloWorld");
                hello.getMessage();
    }
}

HelloWorld.java

package hello;

public class HelloWorld {
        private String message;

        public void setMessage(String message) {
                this.message = message;
        }

        public void getMessage() {
                System.out.println("Message: " + message);
        }
}

的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-3.0.xsd">

   <bean id = "helloWorld" class = "hello.HelloWorld">
      <property name = "message" value = "Hello World!"/>
   </bean>

</beans>

ST

run:
    Mar 20, 2017 11:05:04 PM     org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    INFO: Refreshing     org.springframework.context.support.ClassPathXmlApplicationContext@4ee285c    6: startup date [Mon Mar 20 23:05:04 EDT 2017]; root of context hierarchy
    Mar 20, 2017 11:05:04 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [beans.xml]
    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
        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 hello.MainApp.main(MainApp.java:8)
    Caused by: java.io.FileNotFoundException: class path resource [beans.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
    /home/john/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
    BUILD FAILED (total time: 0 seconds)

File and Project structures

感谢您的帮助。

解决方案:在NetBeans中,用于实例化bean的beans.xml类型文件必须包含它的路径。在我的情况下,我不得不输入:

AbstractApplicationContext context = new ClassPathXmlApplicationContext("/hello/beans.xml");

而不仅仅是:

AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

2 个答案:

答案 0 :(得分:1)

您的beans.xml位于hello个套餐中,因此您应该使用以下代码进行推荐:

AbstractApplicationContext context = new ClassPathXmlApplicationContext("hello/beans.xml");

P.S。:更好的做法是将其放在resources目录中。

答案 1 :(得分:-1)

ResourceLoader 有四种加载资源的方法 (classpath,http,file,none(取决于applicationContext))。 您可以了解更多相关信息