微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法读取Spring XML文件:IOException从类路径资源解析XML文档:嵌套异常为java.io.FileNotFoundException:

如何解决无法读取Spring XML文件:IOException从类路径资源解析XML文档:嵌套异常为java.io.FileNotFoundException:

我已经创建了一个maven项目,并且正在使用Intellij IDE,我试图从Application上下文读取spring bean文件,但未找到File Exception。 详细信息是:

Test.java用于创建ApplicationContext并从springbean.xml文件中读取bean,但是却出现File Not found错误。(异常和线程“ main”

org.springframework.beans.fabeactory.BeanDeFinitionStoreException: IOException parsing XML document from class path resource) Why is it not reading File? I have tried every possible way to read the file but to no avail

即使我已将XML文件放入src / main / resources

这是用于创建应用程序上下文的主要Test类* /

包com.spring.utils;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClasspathXmlApplicationContext;
    
    import java.io.File;
    
    public class Test {
    
        public static void main(String args[])
        {
            File f = new File("springbean.xml");
            System.out.println("Exist test: " + f.exists());
            ApplicationContext context = new ClasspathXmlApplicationContext("src/main/resources/springbean.xml");
            Greeting app = (Greeting) context.getBean("mybean");
            app.greeting();
    
    
    
        }
    }

Springbean.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-3.0.xsd">
        <bean id="mybean" class="com.spring.utils.Greeting"/>
    
    </beans>

enter image description here

以下错误正在得到* /

    Error Logs :
    "C:\Program Files\Java\jdk1.8.0_261\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.3\lib\idea_rt.jar=58548:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_261\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\rt.jar;D:\SpringBoottest\SpringBasicTest\target\classes;C:\Users\akshit73\.m2\repository\org\springframework\spring-core\4.3.10.RELEASE\spring-core-4.3.10.RELEASE.jar;C:\Users\akshit73\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\akshit73\.m2\repository\org\springframework\spring-context\5.2.10.RELEASE\spring-context-5.2.10.RELEASE.jar;C:\Users\akshit73\.m2\repository\org\springframework\spring-aop\5.2.10.RELEASE\spring-aop-5.2.10.RELEASE.jar;C:\Users\akshit73\.m2\repository\org\springframework\spring-beans\5.2.10.RELEASE\spring-beans-5.2.10.RELEASE.jar;C:\Users\akshit73\.m2\repository\org\springframework\spring-expression\5.2.10.RELEASE\spring-expression-5.2.10.RELEASE.jar" com.spring.utils.Test
    Exist test: false
    Exception in thread "main" org.springframework.beans.factory.BeanDeFinitionStoreException: IOException parsing XML document from class path resource [src/main/resources/springbean.xml]; nested exception is java.io.FileNotFoundException: class path resource [src/main/resources/springbean.xml] cannot be opened because it does not exist
        at org.springframework.beans.factory.xml.XmlBeanDeFinitionReader.loadBeanDeFinitions(XmlBeanDeFinitionReader.java:342)
        at org.springframework.beans.factory.xml.XmlBeanDeFinitionReader.loadBeanDeFinitions(XmlBeanDeFinitionReader.java:310)
        at org.springframework.beans.factory.support.AbstractBeanDeFinitionReader.loadBeanDeFinitions(AbstractBeanDeFinitionReader.java:188)
        at org.springframework.beans.factory.support.AbstractBeanDeFinitionReader.loadBeanDeFinitions(AbstractBeanDeFinitionReader.java:224)
        at org.springframework.beans.factory.support.AbstractBeanDeFinitionReader.loadBeanDeFinitions(AbstractBeanDeFinitionReader.java:195)
        at org.springframework.beans.factory.support.AbstractBeanDeFinitionReader.loadBeanDeFinitions(AbstractBeanDeFinitionReader.java:257)
        at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDeFinitions(AbstractXmlApplicationContext.java:128)
        at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDeFinitions(AbstractXmlApplicationContext.java:94)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshbeanfactory(AbstractRefreshableApplicationContext.java:130)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshbeanfactory(AbstractApplicationContext.java:638)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523)
        at org.springframework.context.support.ClasspathXmlApplicationContext.<init>(ClasspathXmlApplicationContext.java:144)
        at org.springframework.context.support.ClasspathXmlApplicationContext.<init>(ClasspathXmlApplicationContext.java:85)
        at com.spring.utils.Test.main(Test.java:14)
    Caused by: java.io.FileNotFoundException: class path resource [src/main/resources/springbean.xml] cannot be opened because it does not exist
        at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
        at org.springframework.beans.factory.xml.XmlBeanDeFinitionReader.loadBeanDeFinitions(XmlBeanDeFinitionReader.java:333)
        
    
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。