没有为元素'context:annotation-config'找到声明

时间:2013-09-14 14:52:55

标签: xml spring spring-mvc

春天,只要我在spring.xml中写<context:annotation-config/>,我就会收到此错误: -

  

线程“main”org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException中的异常:来自类路径资源[spring.xml]的XML文档中的第81行无效;嵌套异常是org.xml.sax.SAXParseException; lineNumber:81; columnNumber:30; cvc-complex-type.2.4.c:匹配的通配符是strict,但是找不到元素'context:annotation-config'的声明。

我的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
xmlns:context="http://www.springframework.org/schema/context"
>

<bean id="circle" class="com.mysite.Circle">
</bean>

 ...

<context:annotation-config/>

有人请告诉我哪里出错了????

4 个答案:

答案 0 :(得分:42)

您正在使用XML命名空间(在本例中为上下文)而未声明它

将您的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"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd">

您也引用了http://www.springframework.org/schema/beans/spring-beans-4.0.xsd,我认为这不存在。

答案 1 :(得分:4)

如果使用Spring 4.0,这就是我发现的内容:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation=
    "http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.0.xsd" 
   xmlns:context="http://www.springframework.org/schema/context">

它对我有用。在此处找到了参考:http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/

答案 2 :(得分:3)

注意:当您使用context命名空间时,您应该使用XML头 2 属性进行更新:

  • xmlns:context
  • xsi:schemaLocation 即可。

初学者过去只会添加xmlns:context,而忘记了xsi:schemaLocation的2个新条目:

<beans xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-4.0.xsd"
......
>

答案 3 :(得分:0)

在xml文件中添加这些行。

<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
http://www.springframework.org/schema/context/spring-context-3.0.xsd">