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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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



I am getting the following errors while trying my first spring project:


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



Here is the applicationContext.xml:


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>



What is causing the error?




9 Answers
9



You have not specified the schema location of the context namespace, that is the reason for this specific error:


<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">





My problem was that I specified wrong schema location. Better double-check it, or copy/paste from here.
– vadipp
Oct 30 '15 at 5:27





The error disappered ONLY when putting the existing "xmlns:context" just before the "xsi:schemaLocation" spec. Thanx for the suggestion.
– tjm1706
Oct 31 '16 at 18:23





+1. This answer could be improved by highlighting what's different between the answer and op's code, or by reducing the example to the pertinent lines, only.
– Madbreaks
Jun 7 at 19:41




This path of the schema location is wrong:


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



The correct path should end with /:


/


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





I am not very sure this is true.
– rslj
Aug 14 '17 at 18:39






The trailing slash is irrelevant to the problem. This answer should not have as many up-votes as it currently has.
– Scarfe
Mar 26 at 7:56



I was having issues with


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'



and for me I had to add the spring-security-config jar to the classpath



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



EDIT:



It might be that you have the correct dependency in your pom.



But...



If you are using multiple spring dependencies and assembling into a single jar then the META-INF/spring.schemas is probably being overwritten by the spring.schemas of another of your spring dependencies.


META-INF/spring.schemas


spring.schemas



(Extract that file from your assembled jar and you'll understand)



Spring schemas is just a bunch of lines that look like this:


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



But if another dependency overwrites that file, then the definition will be retrieved from http, and if you have a firewall/proxy it will fail to get it.



One solution is to append spring.schemas and spring.handlers into a single file.



Check:



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





This sort of helped me to identify the issue I was facing. I had missed adding spring-web dependency in the pom
– ring bearer
Aug 13 '15 at 13:07


spring-web





Also ensure that if you specify a specific version of a Spring schema in the xsi:schemaLocation header, that matches the version(s) of that schema listed in the spring.schemas file and bundled in that Spring jar.
– Matthew Wise
May 10 '16 at 15:43





I had the exactly same issue related to security http. Root cause was a missing spring-security-config jar. adding this jar as compile time maven dependency solved the issue for me.
– nirmalsingh
Jun 28 '17 at 13:45



This error can also be caused if the jar file that contains the XSD you require is not included in your deployed class path.



Make sure the dependencies are available in your container.





I had the issue related to security http explained above in David answer. Root cause was a missing spring-security-config jar. adding this jar as compile time maven dependency solved the issue for me.
– nirmalsingh
Jun 28 '17 at 13:47



If using STS, you can in Eclipse mark the configuration file as "Bean Configuration" file (you can specify that when creating or on right click on a XML file):



Spring Tools > Add as Bean Configuration



You project has to have Spring Nature (right click on maven project for example):



Spring Tools > Add Spring Project Nature



then spring.xml is opened by default with Spring Config Editor


spring.xml



Open With > Spring Config Editor



and this editor has Namespaces tab



Spring Config Editor - Namespaces tab



Which enables you to specify the namespaces:



Spring Config Editor - Namespaces example



Please be aware, that it depends on dependencies (using maven project), so if spring-tx is not defined in maven's pom.xml, option is not there, which prevents you to have The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven' 'context:component-scan' problem...


spring-tx



It's too late but somewhat may useful to others



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



which Means you have Missed some Declarations or The Required Declarations Not Found in Your XML



In my case i forgot to add the follwoing



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



After Adding this the Problem Gone away


<?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">



when you add context:component-scan for the first time in an xml, the following needs to be added.


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">



The correct path shouldn't end with "/", I had it wrong that caused the trouble



Right way:



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



With namespace declaration and schema location you can also check the syntax of the namespace use for example :-


<beans xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation= http://www.springframework.org/`enter code here`schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-driven/> <!-- This is wrong -->
<context:annotation-config/> <!-- This should work -->





While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
– jpp
Aug 8 at 10:04






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard