英文:
I can´t import bean definitions from relative location
问题
以下是翻译好的内容:
在将context-cfg.xml
导入到applicationContext.xml
中时,我遇到了以下错误:
严重: 上下文初始化失败
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: 配置问题:
从URL位置[classpath:/context-cfg.xml]导入bean定义失败
有问题的资源:ServletContext资源[/WEB-INF/applicationContext.xml];嵌套异常是
org.springframework.beans.factory.BeanDefinitionStoreException: 从类路径资源[context-cfg.xml]解析XML文档时出错;
嵌套异常是java.io.FileNotFoundException:无法打开类路径资源[context-cfg.xml],因为它不存在
aplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<import resource="classpath:/context-cfg.xml" /> //这行代码引发了错误
</beans>
我的项目结构如下图所示:
有谁可以帮帮我吗?
英文:
I'm getting the next error when I import context-cfg.xml
in applicationContext.xml
.
GRAVE: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem:
Failed to import bean definitions from URL location [classpath:/context-cfg.xml]
Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from
class path resource [context-cfg.xml]; nested exception is java.io.FileNotFoundException: class path
resource [context-cfg.xml] cannot be opened because it does not exist
aplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<import resource="classpath:/context-cfg.xml" /> //this line throws the error
</beans>
The structure of my project is as follows
Can any one help me?
答案1
得分: 1
从Maven的官方文档中:Maven项目结构
在生成工件的源目录中(即主目录和测试目录),有一个用于Java语言的目录(其中存在正常的包层次结构),以及一个用于资源的目录(根据默认资源定义,将其复制到目标类路径中)。
所以您有两个选项:
选项1:
将您的文件位置更改为Java资源
选项2:
更改语句,如<import resource="classpath:/resources/context-cfg.xml" />
在这种情况下,在Eclipse环境中,您的代码将正常工作,但在Maven构建期间,您需要将资源文件夹添加到类路径中。
英文:
From the official documentation of Maven : Maven project structure
> Within artifact producing source directories (ie. main and test), there is one directory for the language java (under which the normal package hierarchy exists), and one for resources (the structure which is copied to the target classpath given the default resource definition).
So you have two options :
Option 1 :
change the location of your file to Java resources
Option 2 :
change statement like this <import resource="classpath:/resources/context-cfg.xml" />
In this case, your code will work fine in eclipse env, but during your maven build you need to add the resource folder to your classpath
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论