每当我试图运行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>
标准时间
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");
3条答案
按热度按时间ki1q1bka1#
您的
beans.xml
位于hello
套件中,因此您应该使用下列方式来指涉它:resources
目录中。*6pp0gazn2#
还发现如果在Windows上,应该使用双反斜杠(\)。
因此,这是:抽象应用程序上下文上下文=新的类路径XML应用程序上下文(“/hello/beans.xml”)
zi8p0yeb3#
ResourceLoader有四种方法来加载资源**(类路径、http、文件、无(取决于应用程序上下文))。**您可以了解有关它详细信息