【Spring】Spring控制反转IOC创建bean失败-java.io.FileNotFoundException
2018-01-16 00:17
1001 查看
一、起因
今天用spring创建对象,指定了ClassPathXmlApplicationContext的路径,明明就有,为什么会报错找不到xml文件呢,很费解;报错如下:目前未能解决,求大神指点IOException parsing XML document from class path resource [springTest/applicationContexts.xml]; nested exception is java.io.FileNotFoundException: class path resource [springTest/applicationContexts.xml] cannot be opened because it does not exist
二、截图如下
1、applicationContext.xml文件: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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="springTest.User" scope="singleton"> </bean> </beans>
2、主方法截图:
主方法源代码:
package springTest; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("/springTest/applicationContext.xml"); User user1= (User) applicationContext.getBean("user"); User user2= (User) applicationContext.getBean("user"); System.out.println(user1); System.out.println(user2); } }
3、User类截图:
User类源代码:
package springTest; /** * Created by tecpie1 on 2018/1/15. */ public class User { private int id; private String name; private int getId(){ return id; } private void setId(int id){ this.id=id; } public String getName(){ return name; } public void setName(String name){ this.name=name; } }
相关文章推荐
- java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not
- RandomAccessFile创建文件java.io.FileNotFoundException
- Spring错误:java.io.FileNotFoundException: class path resource ..cannot be opened because it does not e
- ssh整合是Caused by: java.io.FileNotFoundException: class path resource [bean/test/User.hbm.xml] cannot
- Android studio混淆打包的时候一直失败,提示Warning:Exception while processing task java.io.FileNotFoundException: D
- java.io.FileNotFoundException: class path resource [spring/ApplicationCont.xml] cannot be opened be
- 异常解决 java.io.FileNotFoundException: class path resource [spring/springmvc.xml]
- java.io.FileNotFoundException: class path resource [templates/] spring-cloud-netflix-eureka-server
- 【Bug解决】Spring配置错误:java.io.FileNotFoundException: class path resource [cxd/entity/User.hbm.xml] canno
- java.io.FileNotFoundException: class path resource [bean/test/User.hbm.xml] cannot be opened because it does not exist
- spring整合mybatis时出现java.io.FileNotFoundException异常
- java.io.FileNotFoundException: D:\Workspaces\MyEclipse 8.5\spring\applicationContext.xml (系统找不到指定的文
- [SSM/Junit]java.io.FileNotFoundException: class path resource [spring-mybatis.xml] cannot be opened
- java.io.FileNotFoundException: class path resource [bean.xml] cannot be opened because it does not e
- java.io.FileNotFoundException: Could not open ServletContext resource [/SpringContext.xml]
- nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mog
- nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mog
- 异常解决 java.io.FileNotFoundException: class path resource [spring/applicationContext_*.xml]
- Caused by: java.io.FileNotFoundException: class path resource [com/ncs/bean/News.hbm.xml] cannot be
- java.io.FileNotFoundException: [WEB-INF/spring-servlet.xml] cannot be opened because it does not exist