2012-11-27 113 views
50

我收到以下錯誤,而試圖我的第一個春天項目:匹配通配符是嚴格的,但沒有聲明可以爲元素的上下文中找到:組件掃描

Caused by: 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 

這裏是applicationContext.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:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 

     <context:component-scan base-package="com.xyz" /> 

</beans> 

什麼導致錯誤?

回答

114

您還沒有指定的上下文命名空間的架構位置,就是這個特定錯誤的原因:

<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-2.5.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd"> 
+1

我的問題是,我指定* wrong *模式位置。最好仔細檢查一下,或從這裏複製/粘貼。 – vadipp

+0

僅當在「xsi:schemaLocation」規範之前放置現有的「xmlns:context」時,該錯誤纔會消失。感謝Thanx建議。 – tjm1706

3

Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'security:http' 

我有問題,對我來說,我不得不將spring-security-config jar添加到類路徑中

http://docs.spring.io/spring-security/site/docs/3.1.x/reference/ns-config.html

編輯:

這可能是你在你的pom中有正確的依賴關係。

但是......

如果您正在使用多個Spring的依賴,並組裝成一個單一的罐子,則META-INF/spring.schemas可能正在被另一個你春天依賴的spring.schemas覆蓋。

(從組裝罐子提取該文件,你就會明白)

春天模式只是一堆看起來像這樣的行:

http\://www.springframework.org/schema/p=org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler 
http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd 

但如果另一依賴覆蓋該文件,那麼定義將從http檢索,並且如果您有防火牆/代理,它將無法獲得它。

一個解決方案是將spring.schemas和spring.handlers追加到一個文件中。

檢查:

Idea to avoid that spring.handlers/spring.schemas get overwritten when merging multiple spring dependencies in a single jar

+0

這種幫助我確定了我面臨的問題。我錯過了在pom中添加'spring-web'依賴 –

+0

還確保如果您在xsi:schemaLocation標頭中指定了Spring模式的特定版本,該版本與spring.schemas中列出的模式的版本相匹配文件並捆綁在該Spring jar中。 –

+0

我有與安全http相同的問題。根本原因是缺少一個spring-security-config jar。添加這個jar作爲編譯時間maven dependency爲我解決了這個問題。 – nirmalsingh

3

如果你部署的類路徑中不包括包含您所需要的XSD的jar文件也可能導致此錯誤。

確保您的容器中存在依賴關係。

+0

我在上面的David答案中解釋了有關安全http的問題。根本原因是缺少一個spring-security-config jar。添加這個jar作爲編譯時間maven dependency爲我解決了這個問題。 – nirmalsingh

2

如果使用STS,你可以在Eclipse標記配置文件作爲「bean配置」文件(你可以指定一個XML文件中創建或右鍵點擊,當):

Spring Tools > Add as Bean Configuration

你項目也有春天,大自然(右擊例如Maven項目):

Spring Tools > Add Spring Project Nature

然後spring.xml默認情況下打開與Spring配置艾迪TOR

Open With > Spring Config Editor

這個編輯器具有命名空間標籤

Spring Config Editor - Namespaces tab

這使您可以指定命名空間:

Spring Config Editor - Namespaces example

請注意,這取決於依賴項(使用maven項目),所以如果spring-tx是不是在Maven的pom.xml中定義的選項是不存在的,這將阻止你有The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven' 'context:component-scan'問題...

5

架構位置的這條道路是錯誤的:

http://www.springframework.org/schema/beans 

正確的路徑應該結束與/

http://www.springframework.org/schema/beans/ 
+0

我不太確定這是否屬實。 – rslj

1

太晚,但有些5月對他人有用

The matching wildcard is strict, but no declaration can be found for element 'context:component-scan

這意味着你必須錯過了一些聲明或所需的聲明未找到在你的XML

在我來說,我忘了加上follwoing

添加此問題走開

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     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 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 
1

當您添加上下文後:在XML組件掃描,第一次,添加了以下需求。

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"> 
相關問題