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

jdg4fx2g  于 2023-11-16  发布在  Spring
关注(0)|答案(5)|浏览(151)

在spring中,每当我在spring.xml中写入<context:annotation-config/>时,我都会得到这个错误:
Exception in thread“main”org.springframework.beans.factory.xml. XmlBeanExceptionStoreException:Class path resource [spring.xml]的XML文档中的第81行无效;嵌套的异常是org.xml.sax.SAXParseException; lineNumber:81; columnNumber:30; cvc-complex-type.2.4.c:匹配的对象是严格的,但找不到元素“context:annotation-bytes”的声明。
我的spring.xml的内容是

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. >
  8. <bean id="circle" class="com.mysite.Circle">
  9. </bean>
  10. ...
  11. <context:annotation-config/>

字符串
有没有人能告诉我,我错在哪里?

pkbketx9

pkbketx91#

您正在使用XML名称空间(在本例中为上下文中),但没有声明它
把你的xml改成:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.1.xsd">

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

to94eoyn

to94eoyn2#

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

  • xmlns:context
    *xsi:schemaLocation

初学者通常只添加xmlns:context,而忘记了xsi:schemaLocation中的两个新条目:

  1. <beans xmlns:context="http://www.springframework.org/schema/context"
  2. xsi:schemaLocation="http://www.springframework.org/schema/beans
  3. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  4. http://www.springframework.org/schema/context
  5. http://www.springframework.org/schema/context/spring-context-4.0.xsd"
  6. ......
  7. >

字符串

uidvcgyl

uidvcgyl3#

如果使用Spring 4.0,以下是我的发现:

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

字符串
它对我很有效。在这里找到了引用:http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/

3bygqnnd

3bygqnnd4#

在xml文件中添加这些行。

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

字符串

  1. <xsi:schemaLocation="http://www.springframework.org/schema/beans
  2. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  3. http://www.springframework.org/schema/context
  4. http://www.springframework.org/schema/context/spring-context-3.0.xsd">


9wbgstp7

9wbgstp75#

最后得到的解决方案只是添加这个包含在XML文件

  1. <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
  2. </bean>

字符串

相关问题