简体   繁体   English

找不到ApplicationContext的Spring配置XML

[英]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.

相关问题 在Spring @Configuration中引用applicationContext.xml bean - Referring applicationContext.xml bean in Spring @Configuration 在带有NetBeans的Spring应用程序中找不到applicationContext.xml - applicationContext.xml not found in Spring application with NetBeans Java Spring ApplicationContext配置 - Java Spring ApplicationContext Configuration 如何将 applicationContext.xml 转换为 spring @Configuration class? - How do I convert an applicationContext.xml into a spring @Configuration class? “此”在Spring XML中引用applicationcontext - “this” reference of applicationcontext in Spring XML 没有XML Spring ApplicationContext - No XML Spring ApplicationContext 如何在web.xml中注册Spring @Configuration带注释的类而不是applicationContext.xml文件? - How to register Spring @Configuration annotated class instead of applicationContext.xml file in web.xml? 春季-applicationContext.xml中的路径 - Spring - Paths in applicationContext.xml Spring安全配置错误/WEB-INF/applicationContext.xml,并且没有名为springSecurityFilterChain的bean - Spring security configuration error /WEB-INF/applicationContext.xml and there's no bean called springSecurityFilterChain spring 引导如何使用 @Autowired 没有 @Component 注释和 xml 配置注入 ApplicationContext 和 JdbcTemplate 的实例? - How does spring boot inject the instance of ApplicationContext and JdbcTemplate using @Autowired without @Component annotation and xml configuration?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM