class path resource [.xml] cannot be opened because it does not exist

发布时间 2023-09-20 15:57:21作者: Lovi*

class path resource [bean1.xml] cannot be opened because it does not exist

错误重现

bug:Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [bean1.xml]; nested exception is java.io.FileNotFoundException: class path resource [bean1.xml] cannot be opened because it does not exist

image-20230807151326126

public static void main(String[] args) {
        //解析beans.xml文件 , 生成管理相应的Bean对象
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //getBean : 参数即为spring配置文件中bean的id .
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }

错误原因

简单,就是明明xml在,但是找不到。

image-20230807151512224

解决方法

image-20230807152321288

这样即可!

image-20230807152348411

成功啦!