Spring框架工作配置问题"元素上下文的前缀上下文:annotation-config未绑定"

zoo*_*oop 8 java xml spring

我有一个奇怪的问题,我似乎无法追查.我有这个与其他服务器一起工作没有问题,但我似乎无法让这个服务器工作.我看到的最接近我的问题的帖子是这篇文章元素"context:component-scan"的前缀"context"没有绑定

所有其他人真的只是因为前缀不在xml文件中.我希望有人能够在这里指出我正确的方向.

Spring 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/>
Run Code Online (Sandbox Code Playgroud)

所以我有,但得到这个错误:

org.xml.sax.SAXParseException: The prefix "context" for element "context:annotation-config" is not bound.
Run Code Online (Sandbox Code Playgroud)

感谢任何帮助.让我知道我还能提供什么.

谢谢

Him*_*ant 17

我遇到了同样的问题,直到我意识到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
...."
Run Code Online (Sandbox Code Playgroud)

然后重建项目.

它运作良好.


Mue*_*uel 13

以下适用于我:

的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>
Run Code Online (Sandbox Code Playgroud)

当我使用以下类来运行它时:

Test.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!");
    }

}
Run Code Online (Sandbox Code Playgroud)

你能看看这个适合你吗?您将在类路径中需要以下库:commons-logging,spring-asm,spring-beans,spring-context,spring-core和spring-expression.

如果有效,请告诉我.如果没有,请发布完整的堆栈跟踪.最后,我使用Spring 3.1.1进行上述操作.


Ani*_*kur 8

当您xmlns:context从spring xml文件中丢失时,会出现此错误.所以添加它.您的bean标题应如下所示 -

<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>
Run Code Online (Sandbox Code Playgroud)