xml 找不到元素“context:annotation-config”的声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18802982/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:10:27  来源:igfitidea点击:

no declaration can be found for element 'context:annotation-config'

xmlspringspring-mvc

提问by sasuke

in spring whenever i write <context:annotation-config/>in my spring.xml i get this error:-

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

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 81 in XML document from class path resource [spring.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 81; columnNumber: 30; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element '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”的声明。

And the content of my spring.xml is

而我的 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/>

Would anyone please tell me where am i going wrong ????

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

回答by Frederic Close

You are using an XML namespace (in this case context) without declaring it

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

Change your xml to this:

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

You were also referencing http://www.springframework.org/schema/beans/spring-beans-4.0.xsd, which I don't think exists.

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

回答by radistao

Take attention: when you use contextnamespace, you should update in XML head 2attributes:

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

  • xmlns:context
  • xsi:schemaLocation.
  • xmlns:context
  • xsi:schemaLocation.

Beginners used to add only xmlns:contextand forget about 2 new entries to xsi:schemaLocation:

初学者过去常常只添加xmlns:context而忘记了 2 个新条目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"
......
>

回答by haileymow

If using Spring 4.0, here's what I found:

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

It worked for me. Found the reference here: http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/

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

回答by Yash

Add these lines in your xml file.

在您的 xml 文件中添加这些行。

<xmlns:context="http://www.springframework.org/schema/context"

And

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

.

.