Java Spring框架工作配置问题“元素上下文的前缀上下文:注释配置未绑定”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18137667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring frame work config issue "prefix context for for element context:annotation-config is not bound"
提问by zooppoop
I am having a weird issue that I can't seem to track down. I have this working with other servers without a problem, but I can't seem to get this one to work. The closest post that I see to my problem was this post The prefix "context" for element "context:component-scan" is not bound
我有一个奇怪的问题,我似乎无法追踪。我有这个与其他服务器一起工作没有问题,但我似乎无法让这个工作。我看到的最接近我的问题的帖子是这个帖子元素“context:component-scan”的前缀“context”没有绑定
All others really were just because the prefix was not in the xml file. I am hoping someone might be able to point me in the right direction here.
所有其他人真的只是因为前缀不在 xml 文件中。我希望有人能在这里指出我正确的方向。
Spring XML file:
弹簧 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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<context:annotation-config/>
So I have that, but getting this error:
所以我有,但收到此错误:
org.xml.sax.SAXParseException: The prefix "context" for element "context:annotation-config" is not bound.
Appreciate any help. Let me know what else I can provide.
感谢任何帮助。让我知道我还能提供什么。
Thanks
谢谢
回答by Muel
The following works for me:
以下对我有用:
test.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<context:annotation-config/>
</beans>
When I use the following class to run it:
当我使用以下类来运行它时:
Test.java
测试.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws Exception {
new ClassPathXmlApplicationContext("test.xml");
System.out.println("Finished!");
}
}
Can you please see if this runs for you? You will need the following libraries in the classpath: commons-logging, spring-asm, spring-beans, spring-context, spring-core, and spring-expression.
你能看看这是否适合你吗?您将需要类路径中的以下库:commons-logging、spring-asm、spring-beans、spring-context、spring-core 和 spring-expression。
Please let me know if it worked. If it didn't, please post the full stack-trace. Finally, I used Spring 3.1.1 for the above.
请让我知道它是否有效。如果没有,请发布完整的堆栈跟踪。最后,我在上面使用了 Spring 3.1.1。
回答by Himadri Pant
I was experiencing the same problem until I realized beans
tag attribute xmlns:context
was missing. Just added the below lines
我遇到了同样的问题,直到我意识到缺少beans
标签属性xmlns:context
。刚刚添加了以下几行
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
...."
Then rebuilt the project.
然后重建项目。
It worked well then on.
当时效果很好。
回答by Aniket Thakur
This error comes when you have xmlns:context
missing from your spring xml file. So add it. Your beans header should look something like the following -
当您xmlns:context
从 spring xml 文件中丢失时,就会出现此错误。所以添加它。您的 beans 标头应如下所示 -
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd" >
<context:annotation-config />
<context:component-scan base-package="controller" />
</beans>
回答by Olakunle Awotunbo
I encountered the same problem but i was able to solve it by moving
我遇到了同样的问题,但我能够通过移动来解决它
from applicationContext.xml to spring-servlet.xml and adding xmlns:context in the spring-servlet.xml
从 applicationContext.xml 到 spring-servlet.xml 并在 spring-servlet.xml 中添加 xmlns:context