我无法从相对位置导入Bean定义。

huangapple go评论65阅读模式
英文:

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>

我的项目结构如下图所示:

我无法从相对位置导入Bean定义。

有谁可以帮帮我吗?

英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:p=&quot;http://www.springframework.org/schema/p&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot; xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
xsi:schemaLocation=&quot;
   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&quot;&gt;


&lt;import resource=&quot;classpath:/context-cfg.xml&quot; /&gt; //this line throws the error

&lt;/beans&gt;

The structure of my project is as follows

我无法从相对位置导入Bean定义。

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 &lt;import resource=&quot;classpath:/resources/context-cfg.xml&quot; /&gt;
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

huangapple
  • 本文由 发表于 2020年9月2日 20:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63705956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定