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

sas*_*uke 25 xml spring spring-mvc

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

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

有谁请告诉我我哪里错了????

Fre*_*ose 43

您正在使用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">
Run Code Online (Sandbox Code Playgroud)

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


rad*_*tao 5

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

  • xmlns:context
  • 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"
......
>
Run Code Online (Sandbox Code Playgroud)