2012-07-11 48 views
4

我從Spring 3(3.1.2)和Google App Engine開始。元素的上下文沒有聲明:component-scan'

我跟着一個tutoria在線,現在,我有我的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,他們似乎是正確的。

+0

把-3.1的xsd不會改變什麼 – EsseTi 2012-07-11 20:28:18

回答

1

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

+0

我實際上添加所有的庫只是爲了確保。但通知改變了。 PS:沒有「春天上下文」,有「上下文」,「context.support」 – EsseTi 2012-07-12 08:41:22

+0

聽起來不正確@EsseTi,應該有一個spring-context-3.1.2.RELEASE.jar,除非你是使用Spring分發的osgi jar。 – 2012-07-12 12:12:19

+0

你能提供一個從哪裏下載罐子的鏈接嗎? 無論如何,問題出現在XML中,因爲我沒有關閉'「,但在文件末尾(我從一個在線示例)。 – EsseTi 2012-07-12 15:26:14

1

我不知道這是否有助於任何,但我同時使用maven shade plugin,使單一的.jar和陰影插件殲滅從META-INF/spring.handlersspring.schemas有非常相似,你的錯誤。

這SO答案我指出了正確的方向:https://stackoverflow.com/a/3650844/7008

以及使用的AppendingTransformer添加那些春季位回您META-INF/文件夾的伎倆文件對我來說:http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#AppendingTransformer

+0

這對我有用。請考慮將Maven變壓器配置添加到答案中。 – 2017-01-20 18:20:37

1

問題是與您的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版本。這個錯誤可能是由於這個事實也...