gpt4 book ai didi

Java Spring : Why does moving XML file inside the package gives me an error

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:06 24 4
gpt4 key购买 nike

我是 Java Spring 新手。

我创建了一个名为 com.MyFirstSpringPackage 的包,其中有几个 Java 文件,还有一个 XML 文件(包外部),它们调用类构造函数(连接 bean),一切都很顺利。

现在我决定使用 eclipse 通过拖放操作将此 XML 文件推送到包内,然后程序开始抛出错误:

这是堆栈跟踪:

Jun 17, 2014 11:03:12 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@15327b79: startup date [Tue Jun 17 11:03:12 IST 2014]; root of context hierarchy
Jun 17, 2014 11:03:12 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [FirstSprings.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [FirstSprings.xml]; nested exception is java.io.FileNotFoundException: class path resource [FirstSprings.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.MyFirstSpringPackage.MainSpring.main(MainSpring.java:10)
Caused by: java.io.FileNotFoundException: class path resource [FirstSprings.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:329)

这个错误的原因是什么?为什么我不能将它移到包内,包只是一个文件夹?

最佳答案

您可以尝试使用包名称来引用 xml 文件,例如:

String xmlFile = "com/MyFirstSpringPackage/FirstSprings.xml";

关于Java Spring : Why does moving XML file inside the package gives me an error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24256387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com