85

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?

Betlista
  • 10,327
  • 13
  • 69
  • 110
user93796
  • 18,749
  • 31
  • 94
  • 150

17 Answers17

170

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">
kuporific
  • 10,053
  • 3
  • 42
  • 46
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • 4
    My problem was that I specified *wrong* schema location. Better double-check it, or copy/paste from here. – vadipp Oct 30 '15 at 05:27
  • The error disappered ONLY when putting the existing "xmlns:context" just before the "xsi:schemaLocation" spec. Thanx for the suggestion. – tm1701 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 07 '18 at 19:41
  • 1
    I was following course "O'Reilly - Spring Framework Essentials" and had such error. Turns out there was a typo in a course: instead of proper "http://www.springframework.org/schema/context/spring-context.xsd" they had a wrong "http://www.springframework.org/schema/beans/spring-context.xsd". Not blaming them, as nothing is perfect, but posting it here, maybe it'll help someone. – AbstractVoid Nov 24 '18 at 20:48
7

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:

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

Community
  • 1
  • 1
David Rz Ayala
  • 2,165
  • 1
  • 20
  • 22
  • 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
  • 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
6

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/
arghtype
  • 4,376
  • 11
  • 45
  • 60
Sahil Bhalla
  • 175
  • 1
  • 4
5

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.

sweetfa
  • 5,457
  • 2
  • 48
  • 62
  • 1
    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
4

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">
RAJESH KUMAR ARUMUGAM
  • 1,560
  • 21
  • 35
3

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 The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven' 'context:component-scan' problem...

Community
  • 1
  • 1
Betlista
  • 10,327
  • 13
  • 69
  • 110
3

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

Mutaealim
  • 127
  • 11
2

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

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

mp31415
  • 6,531
  • 1
  • 44
  • 34
1

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

techguy
  • 61
  • 1
  • 2
1

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 -->
Draken
  • 3,134
  • 13
  • 34
  • 54
Amit
  • 633
  • 6
  • 13
1

Add xmlns context with http as shown below

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

Manjunatha B
  • 276
  • 3
  • 9
1
<?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>
1

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
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
Sunrays
  • 41
  • 1
  • 3
1

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);
    ...
}
Piotr Wilkin
  • 3,446
  • 10
  • 18
0

changing xsi:schemalocation to following solved my issue

    <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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation=
    "http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
Devesh
  • 1
  • 1
0

I tried all solution but it doesn't work. Finally found that in xsi:schemaLocation for context URI, it must be http and not https.