找不到context.xml文件

时间:2017-02-20 14:01:11

标签: java xml spring intellij-idea

在Spring的Java框架中,我使用context.xml文件创建bean,然后使用ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");加载它。

我的程序抛出异常java.io.FileNotFoundException: class path resource [context.xml] cannot be opened because it does not exist

该文件存在。我使用Gradle进行依赖项管理,我的src文件夹(文件所在的位置)在Intellij中被标记为资源。据我所知,即使我的src文件夹应包含在classpath变量中,gradle run task也无法从此处获取它。

当我使用new FileSystemXmlApplicationContext("context.xml");时,一切正常,但我确实需要使用" Classpath"我的任务。

英语不是我的母语。

这是我的#34; Main"类:

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new FileSystemXmlApplicationContext("context.xml");
    }
}

这是来自context.xml文件的bean

<bean id="t1000" class="by.zti.main.impl.T1000">
    <constructor-arg ref="sonyHead"/>
    <constructor-arg ref="toshibaLeg"/>
</bean>

<bean id="sonyHead" class="by.zti.main.impl.SonyHead"/>
<bean id="sonyLeg" class="by.zti.main.impl.SonyLeg"/>
<bean id="toshibaHead" class="by.zti.main.impl.ToshibaHead"/>
<bean id="toshibaLeg" class="by.zti.main.impl.ToshibaLeg"/>

这是我的build.gradle文件

group 'zti.spring.test'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'idea'

sourceCompatibility = 1.8
targetCompatibility = 1.8

mainClassName = 'by.zti.main.Main'

repositories {
    mavenCentral()
}

jar {
    baseName = 'gs-gradle'
    version = '0.1.0'
}

dependencies {
    compile 'org.springframework:spring-context:4.3.6.RELEASE'
    compile fileTree(dir:'src', includes:  ['/*.xml,/*.properties'])
}

例外文字

    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [context.xml]; nested exception is java.io.FileNotFoundException: class path resource [context.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.(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
    at by.zti.main.Main.main(Main.java:12)
Caused by: java.io.FileNotFoundException: class path resource [context.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

这是我项目中的文件树

enter image description here

我的类路径的内容

/C:/Users/Cvazer/IdeaProjects/spring-test/build/classes/main/
/C:/Users/Cvazer/IdeaProjects/spring-test/build/resources/main
/C:/Users/Cvazer/.gradle/caches/modules-2/files-2.1/org.springframework/spring-context/4.3.6.RELEASE/5f3041020113e3202a7ae9defe36e7b5d2fb87eb/spring-context-4.3.6.RELEASE.jar
/C:/Users/Cvazer/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aop/4.3.6.RELEASE/d70b2393d380cd468d9a14969efaf9022c67c0/spring-aop-4.3.6.RELEASE.jar
/C:/Users/Cvazer/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/4.3.6.RELEASE/1585715ed598b76a23dac931c27aa039b189eafb/spring-beans-4.3.6.RELEASE.jar
/C:/Users/Cvazer/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.3.6.RELEASE/690da099c3c2d2536210f0fd06ff3f336de43ad9/spring-core-4.3.6.RELEASE.jar
/C:/Users/Cvazer/.gradle/caches/modules-2/files-2.1/org.springframework/spring-expression/4.3.6.RELEASE/13b53568cfd7b308e70efcbac6cdd0c5d597ba1/spring-expression-4.3.6.RELEASE.jar
/C:/Users/Cvazer/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar

2 个答案:

答案 0 :(得分:0)

我很瘦,你应该把文件放在src/main/resources/中,或者指定另一条路径ClassPathXmlApplicationContext("src/main/resources/blablabla/context.xml")

答案 1 :(得分:0)

你应该附加“file:”,即你的网址应该是这样的“file:src / main / resources / filename.xml”