想在本地启动Spring,然后从框架中获得一个bean,于是就有了下面的故事,对于一个程序猿来说这样的异常是不能容忍的,为什么?因为特么介个异常是异常界的屌丝,一个词:低级!忠告:不过,你最好表这样子!千万不要瞧不起!
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.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.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:79) at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:67) at com.dtxy.pool.StartSpring.getConnectionManager(StartSpring.java:44) at com.dtxy.pool.StartSpring.main(StartSpring.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) Caused by: java.io.FileNotFoundException: class path resource [applicationContext.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) ... 10 more此异常问题很明显,就是在classpath下找不到applicationContext.xml文件。
下面我们就来看一下,几种加载方式,以及路径说明:
private static String path = "D:\\workspaceSpark\\src\\main\\resources\\applicationContext.xml"; private static String classpath = "applicationContext.xml"; //通过XmlBeanFactory获取bean,通过ClassPathResource类加载applicationContext,从类名知道applicationContext.xml必须位于classpath下 XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(classpath)); ConnectionManager connectionManager = (ConnectionManager) factory.getBean("connectionManager"); //通过ApplicationContext上下文获取bean,通过FileSystemXmlApplicationContext类加载applicationContext,可同时加载多个xml文件,从类名知道只要路劲正确就可以将applicationContext.xml加载 ApplicationContext context = new FileSystemXmlApplicationContext(new String[] {path}); ConnectionManager connectionManager = (ConnectionManager) context.getBean("connectionManager"); //通过ApplicationContext上下文获取bean,通过ClassPathXmlApplicationContext类加载applicationContext,可同时加载多个xml文件,从类名知道applicationContext.xml必须位于classpath下 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ConnectionManager connectionManager = (ConnectionManager) context.getBean("connectionManager");
注:很多人总搞不清楚classpath到底是哪里,Java Project项目其实就是指的src目录,如果是maven项目就是指的resources。
分享:
上一篇javax.naming.NamingException: No naming context bound to this class loader
崇尚极简,热爱技术,喜欢唱歌,热衷旅行,爱好电子产品的一介码农。
联系QQ:58742094
联系电话:
工作邮箱:
当你的才华还撑不起你的野心的时候,你就应该静下心来学习,永不止步!
人生之旅历途甚长,所争决不在一年半月,万不可因此着急失望,招精神之萎葸。
Copyright 2015- 芒果酷(mangocool.com) All rights reserved. 湘ICP备14019394号
免责声明:本网站部分文章转载其他媒体,意在为公众提供免费服务。如有信息侵犯了您的权益,可与本网站联系,本网站将尽快予以撤除。