【Spring】Spring控制反转IOC创建bean失败-java.io.FileNotFoundException

2018年1月16日 13点热度 0条评论 来源: 张牧野菌

一、起因

今天用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;
    }
}
    原文作者:张牧野菌
    原文地址: https://blog.csdn.net/River_Continent/article/details/79070234
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。