[英]Spring configuration XML not found for ApplicationContext
I have this very simple project made with NetBeans 8.1 (new project > Maven > Java application): 我有一个使用NetBeans 8.1进行的非常简单的项目(新项目> Maven> Java应用程序):
Here's App.java
: 这是App.java
:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
System.out.println("why");
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
MyObject obj = (MyObject) context.getBean("myobj");
obj.getMessage();
}
}
This is MyObject.java
: 这是MyObject.java
:
public class MyObject {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
And Beans.xml
: 和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="myobj" class="asd.spring1.MyObject">
<property name="message" value="Hello World!"/>
</bean>
</beans>
pom.xml
: pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>asd</groupId>
<artifactId>Spring1</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
</dependencies>
</project>
If I try to run the project I get FileNotFoundException
: 如果我尝试运行项目, FileNotFoundException
收到FileNotFoundException
:
why
Dec 25, 2016 7:05:13 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4d405ef7: startup date [Sun Dec 25 19:05:13 EET 2016]; root of context hierarchy
Dec 25, 2016 7:05:13 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 asd.spring1.App.main(App.java:11)
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
As you can see I have put Beans.xml
everywhere. 如您所见,我将Beans.xml
放在各处。 Do you know why am I getting error and how can I fix this? 您知道我为什么会出错并且如何解决此问题?
You can check compiled JAR that it doesn't contain Beans.xml
. 您可以检查已编译的JAR,使其不包含Beans.xml
。 See explanation below. 请参阅下面的说明。
First of all, you shouldn't place non-java files to src/main/java
directory. 首先,您不应将非Java文件放置到src/main/java
目录中。
Secondly, you can check Maven's build output (default invocation of resources plugin): it uses src/main/resources
directory to search resources by default. 其次,您可以检查Maven的build输出(默认调用资源插件):默认情况下,它使用src/main/resources
目录搜索资源。
As a result, you should to place your XML files to src/main/resources
directory. 因此,您应该将XML文件放置在src/main/resources
目录中。
See Maven Resources Plugin for more details. 有关更多详细信息,请参见Maven资源插件 。
Do you have your Beans.xml inside target folder, you need to have your xml file somewhere inside directory where your compiled classes are. 您的目标文件夹中是否有Beans.xml,需要将xml文件的位置放在已编译类所在的目录中。
Also if you have building and running your app with ID (so it compiles and moves classes by default in some folder), you can also try to put 另外,如果您已使用ID构建和运行您的应用(因此默认情况下它会在某些文件夹中编译和移动类),那么您也可以尝试将
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:Beans.xml");
to be sure to check all packages in classpath for your config class. 确保为您的配置类检查classpath中的所有软件包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.