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

本文记录了一次使用Spring框架过程中遇到的问题:通过ClassPathXmlApplicationContext加载配置文件时出现FileNotFoundException异常,详细展示了applicationContext.xml配置文件、主方法及User类代码,并探讨了可能的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、起因

今天用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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陶洲川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值