【问题标题】:no declaration can be found for element 'context:annotation-config'找不到元素“上下文:注释配置”的声明
【发布时间】:2013-09-19 03:00:16
【问题描述】:

在春天,每当我在 spring.xml 中写 <context:annotation-config/> 时,我都会收到此错误:-

线程“main”中的异常 org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:来自类路径资源 [spring.xml] 的 XML 文档中的第 81 行无效;嵌套异常是 org.xml.sax.SAXParseException;行号:81;列号:30; cvc-complex-type.2.4.c:匹配通配符是严格的,但找不到元素'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/>

谁能告诉我哪里出错了????

【问题讨论】:

标签: xml spring spring-mvc


【解决方案1】:

您正在使用 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,我认为它不存在。

【讨论】:

  • 这对我有用。但是有人可以准确地解释一下错误是什么以及这些命名空间如何解决它。在之前的声明中,我们还定义了 xmlns:context="springframework.org/schema/context"。
【解决方案2】:

如果使用 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/

【讨论】:

    【解决方案3】:

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

    • xmlns:context
    • xsi:schemaLocation

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

    <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"
    ......
    >
    

    【讨论】:

      【解决方案4】:

      在您的 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">
      

      .

      【讨论】:

        猜你喜欢
        • 2016-06-15
        • 1970-01-01
        • 2012-07-11
        • 2019-03-24
        • 1970-01-01
        • 2012-04-11
        • 2016-01-02
        • 2012-07-02
        • 2012-11-28
        相关资源
        最近更新 更多