“cvc-complex-type.2.4.c: matching wildcard is strict, but no declaration can be found for element "

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dingchenxixi/article/details/80655393

配置xml文件时,错误显示:
“cvc-complex-type.2.4.c: matching wildcard is strict, but no declaration can be found for element ‘context:component-scan’.”
原因是缺少:

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

一般报这种错是缺少命名空间配置,具体查找方式:
spring-aop-4.3.3.RELEASE.jar为例


找到MET-INF这个文件夹

Spring.handlers

http\://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler

spring.schemas

http\://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd
http\://www.springframework.org/schema/aop/spring-aop-4.0.xsd=org/springframework/aop/config/spring-aop-4.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-4.1.xsd=org/springframework/aop/config/spring-aop-4.1.xsd
http\://www.springframework.org/schema/aop/spring-aop-4.2.xsd=org/springframework/aop/config/spring-aop-4.2.xsd
http\://www.springframework.org/schema/aop/spring-aop-4.3.xsd=org/springframework/aop/config/spring-aop-4.3.xsd
http\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-4.3.xsd

spring.tooling

Tooling related information for the aop namespace
http\://www.springframework.org/schema/aop@name=aop Namespace
http\://www.springframework.org/schema/aop@prefix=aop
http\://www.springframework.org/schema/aop@icon=org/springframework/aop/config/spring-aop.gif

看见上面的地址,都是http的,那么肯定会用到互联网,那断网了怎么办呢?注意一下,每个地址的后面都有一个本地地址(标明每个文件所在的本地位置)
那如果www.springframework.org访问不到,Spring如何加载XSD来验证XML配置文件呢?

简短接说,Spring会注册一个自己的EntityResolver,然后当在XML文档中遇到XSD的systemID,也就是XSD文件的URL时,如:http://www.springframework.org/schema/aop/spring-aop-4.3.xsd,Spring的EntityResolver就会通过spring的jar包中META-INF/spring.schemas文件来做一个查找。Spring的4.3.X版本的jar包中的META-INF/spring.schemas文件内容(上面所示)
注意,这个文件中有两类URL,一类带有版本号2.0-4.3,一类不带。如果在这个文件中能查找到,Spring将直接加载jar包中打包的XSD文件,而无需访问公网的www.springframework.org。

但是,如果你“悲剧”地在XSD的URL中写了版本号,但是这个版本号与classpath中spring的版本不相符合。比如,classpath中的spring的jar包版本是2.0.1,而你在XML文件中写的XSD的systemID是:http://www.springframework.org/schema/aop/spring-aop-4.3.xsd,那Spring就无法通过spring.schemas文件找映射到jar包中的XSD文件。就只能去公网上查找加载这个spring-aop-4.3.xsd文件。如果这时www.springframework.org刚好被墙了,访问不了,那么Spring容器就会报错,类似“Failed to read schema document”,容器就无法初始化成功了。

所以正确的写法应该是XSD的URL中不要加版本号(这样以后Spring的版本升级了,也不要紧了)。如果公司内部很多应用的Spring配置文件都已经在用加了版本号的XSD文件URL的话,那么一种比较土的解决方法是,自己搭建一个web服务器,把所有相关的XSD文件都放上去,然后在应用的机器hosts文件中,将www.springframework.org绑定到自己搭建的web服务器上去。这样,就不依赖公网的XSD文件了。

参考文章:
https://blog.csdn.net/gjyalpha/article/details/7307044

猜你喜欢

转载自blog.csdn.net/dingchenxixi/article/details/80655393
今日推荐