找不到元素'context:annotation-config'的声明


问题内容

在spring,每当我<context:annotation-config/>在spring.xml中编写时,都会出现此错误:

线程“主”中的异常org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:来自类路径资源[spring.xml]的XML文档中的第81行无效;嵌套的异常是org.xml.sax.SAXParseException;
lineNumber:81; columnNumber: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命名空间(在这种情况下为上下文)而未声明它

将您的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
,我认为它不存在。