【问题标题】:No declaration can be found for element 'context:component-scan'找不到元素“上下文:组件扫描”的声明
【发布时间】:2012-07-11 13:25:41
【问题描述】:

我从 Spring 3 (3.1.2) 和 Google App Engine 开始。

我在网上学习了一个教程,现在,我的 bean-realted 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.xsd
http://www.springframework.org/schema/context/
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="my.example">

所有java文件的根包都是“my.example”。子包是带有子包的“模型”和“控制器”。 现在。

当我启动应用程序时,我收到此错误:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from ServletContext resource [/WEB-INF/spring-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'.

有更多的堆栈跟踪。

有谁知道我该如何解决这个问题? 我检查了 XSD,它们似乎是正确的。

【问题讨论】:

  • 为 xsd 设置 -3.1 不会改变任何东西

标签: spring google-app-engine


【解决方案1】:

您可能缺少 spring-context.jar,它包含上下文模式的定义。

【讨论】:

  • 我实际上添加了所有的库只是为了确定。但什么都没有改变。 PS:没有“spring-context”,有“context”,“context.support”
  • 听起来不对@EsseTi,应该有一个 spring-context-3.1.2.RELEASE.jar ,除非您使用的是 Spring 分发的 osgi jar。
  • 你能提供一个下载jar包的链接吗?无论如何,问题出在 XML 中,因为我没有立即关闭 &lt;context:component-scan base-package="my.example"&gt;,而是在文件末尾(我从一个在线示例中获取)。
【解决方案2】:

我不知道这是否有帮助,但我在使用 maven shade plugin 制作单个 .jar 时遇到了与您非常相似的错误,并且 shade 插件从 spring.handlersspring.schemas 中删除META-INF/.

这个 SO 答案为我指明了正确的方向:https://stackoverflow.com/a/3650844/7008

关于使用 AppendingTransformer 将这些弹簧位添加回您的 META-INF/ 文件夹的文档对我来说很有用:http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#AppendingTransformer

【讨论】:

  • 这对我有用。请考虑将 maven 变压器配置添加到答案中。
【解决方案3】:

问题是架构位置与您的 spring-context 依赖项不兼容。

替换

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/context/spring-context-2.5.xsd

这应该可以解决问题!我使用的是 spring-context 3.1.1,但在提到 http://www.springframework.org/schema/context/spring-context-2.1.xsd

时遇到了同样的错误

但是,您还应该做一个 maven 树依赖并检查您是否有不同的 spring 版本。这个错误也可能是由于这个事实......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-14
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2012-11-28
    • 2012-09-14
    相关资源
    最近更新 更多