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

JavaSpring

Java Problem Overview


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:

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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

(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:

https://stackoverflow.com/questions/5586515/idea-to-avoid-that-spring-handlers-spring-schemas-get-overwritten-when-merging-m

Solution 3 - Java

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/

Solution 4 - Java

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.

Solution 5 - Java

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

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 https://stackoverflow.com/questions/34779551/the-matching-wildcard-is-strict-but-no-declaration-can-be-found-for-element-tx/34789179#34789179 problem...

Solution 6 - Java

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

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

Solution 7 - Java

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

Solution 8 - Java

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

Solution 9 - Java

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

Solution 10 - Java

Add xmlns context with http as shown below

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

Solution 11 - Java

<?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-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

The above links need to be included

	<context:property-placeholder location="classpath:sport.properties" />


	<bean id="myFortune" class="com.kiran.springdemo.HappyFortuneService"></bean>

	<bean id="myCoach" class="com.kiran.springdemo.setterinjection.MyCricketCoach">
		<property name="fortuner" ref="myFortune" />

		<property name="emailAddress" value="${ipl.email}" />
		<property name="team" value="${ipl.team}" />

	</bean>


</beans>

Solution 12 - Java

Add This Two Schema locations. That's enough and Efficient instead of adding all the unnecessary schema

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

Solution 13 - Java

There are 'META-INF/spring.schemas' files in various Spring jars containing the mappings for the URLs that are intercepted for local resolution. If a particular xsd URL is not listed in these files (for example after switching from http to https) Spring tries to load schemas from the Internet and if the system has no Internet connection it fails and causes this error.

This can be the case with Spring Security v5.2 and up where there is no http mapping for the xsd file.

To fix it change

xsi:schemaLocation="
	http://www.springframework.org/schema/beans     
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/security  
	http://www.springframework.org/schema/security/spring-security.xsd"
	

to

xsi:schemaLocation="
	http://www.springframework.org/schema/beans     
	https://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/security  
	https://www.springframework.org/schema/security/spring-security.xsd"

Note that only actual xsd URL was modified from http to https (only two places above).

Solution 14 - Java

Change http to https of the marked error or all the URL ending with *.xsd extension.

Solution 15 - Java

There is one possibility here which has not been mentioned, but I believe is important.

Normally, when you perform validation, it should be done with respect to the xsi:schemaLocaation parameter provided, i.e. if the validator doesn't have the schema loaded in context, it tries to offload it from the location provided. However, by default, the Xerces parser used by Java enables a flag called USE_GRAMMAR_ONLY_POOL_FEATURE (http://apache.org/xml/features/internal/validation/schema/use-grammar-pool-only), which disables any offloading of schemas. Therefore, if you haven't preloaded your schema in the grammar pool of your validator, you will encounter that message.

What further complicates the issue is that the feature cannot be enabled in the validator directly. You have to enable it in the wider scope of the schema factory:

SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
    sf.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.USE_GRAMMAR_POOL_ONLY_FEATURE, false);
    ...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser93796View Question on Stackoverflow
Solution 1 - JavaBiju KunjummenView Answer on Stackoverflow
Solution 2 - JavaDavid Rz AyalaView Answer on Stackoverflow
Solution 3 - JavaSahil BhallaView Answer on Stackoverflow
Solution 4 - JavasweetfaView Answer on Stackoverflow
Solution 5 - JavaBetlistaView Answer on Stackoverflow
Solution 6 - JavaRAJESH KUMAR ARUMUGAMView Answer on Stackoverflow
Solution 7 - JavaharishpatarlaView Answer on Stackoverflow
Solution 8 - JavatechguyView Answer on Stackoverflow
Solution 9 - JavaAmitView Answer on Stackoverflow
Solution 10 - JavaManjunatha BView Answer on Stackoverflow
Solution 11 - JavaVanga Kiran Kumar ReddyView Answer on Stackoverflow
Solution 12 - JavaSunraysView Answer on Stackoverflow
Solution 13 - Javamp31415View Answer on Stackoverflow
Solution 14 - JavaMutaealimView Answer on Stackoverflow
Solution 15 - JavaPiotr WilkinView Answer on Stackoverflow