您的位置:首页 > 编程语言 > Java开发

【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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐