IOException parsing XML document from class path resource resource [com/zj/spring01/helloworld/appli

前端之家收集整理的这篇文章主要介绍了IOException parsing XML document from class path resource resource [com/zj/spring01/helloworld/appli前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

之前学习Spring的时候一直用的Myeclipse和eclipse编辑器,将配置文件直接放进当前Project下进行加载解析就行了。但是换成IEDA以后出现了一些问题。重温Spring知识点的时候,我加入相应的依赖之后,编辑类App1_get_ioc,进行单元测试后,输出错误为:

十一月 15,2017 6:35:09 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@606d8acf: startup date [Wed Nov 15 18:35:09 CST 2017]; root of context hierarchy
十一月 15,2017 6:35:09 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/zj/spring01/helloworld/applicationContext.xml]

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/zj/spring01/helloworld/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/zj/spring01/helloworld/applicationContext.xml] cannot be opened because it does not exist

	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
	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:130)
	at org.springframework.context.support.AbstractApplicationContext.obtainFreshbeanfactory(AbstractApplicationContext.java:537)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at com.zj.spring01.helloworld.App1_get_ioc.testIOC(App1_get_ioc.java:22)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:44)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
	at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:159)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.io.FileNotFoundException: class path resource [com/zj/spring01/helloworld/applicationContext.xml] cannot be opened because it does not exist
	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
	... 36 more
App1_get_ioc.java

package com.aj.spring01.helloworld;

        import org.junit.Test;
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;


public class App1_get_ioc {
    //1.通过工厂类得到IOC容器创建的对象
    @Test
    public void testIOC() throws Exception{
        //创建对象
        //现在把创建对象交给spring容器
        ApplicationContext ac = new ClassPathXmlApplicationContext
                ("com/aj/spring01/helloworld/applicationContext.xml");
        User user=(User)ac.getBean("user");
        user.setId(100);
        user.setName("good boy!");
        System.out.println(user);
        System.out.println(user.getId());
        System.out.println(user.getName());
    }
}
applicationContext.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
	
	<!-- IOC容器的配置: 要创建的所有的对象都配置在这里
	<bean id="user" class="cn.itcast.a_hello.User" init-method="init_user" destroy-method="destroy_user" scope="singleton" lazy-init="false"></bean>
	 -->
	<bean id="user" class="com.aj.spring01.helloworld.User" init-method="init_user" destroy-method="destroy_user" scope="singleton" lazy-init="false"></bean>
	<bean id="user1" class="com.aj.spring01.helloworld.User"></bean>
</beans>
异常说明的也比较清楚,就是指定的路径不存在!我也很好奇,之前在eclipse上运行完全没问题,可是到了IDEA上运行就出现问题了。于是查询了关于ClassPathXmlApplicationdContext的一些用法ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]。ClassPathXmlApplicationContext 默认从类路径加载配置文件,在类路径文件夹下。这里用的maven的文件夹结构,如下图:


target文件夹就是存放class类的文件夹相应目录下,
可是其下面并没有applicationContext.xml文件。IDEA在源码目录(src/main/java),只会把.java文件编译成.class文件,存到classes文件夹下,其他的配置文件是不会做处理的(MyEclipse会把配置文件复制到相应的包下)。但可以在resources文件夹下,创建相应的包目录,放到相应的目录下,IDEA就会将配置文件,复制到classes文件夹下,也就可以访问到了。 在这里只需将applicationContext.xml复制到target目录下的helloworld下即可,然后再次Junit编译,结果如下:

原文链接:https://www.f2er.com/xml/293627.html

猜你在找的XML相关文章