我在尝试第一个春季项目时遇到以下错误:
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>
是什么导致错误?
您尚未指定上下文命名空间的架构位置,这是此特定错误的原因:
<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">
使用http添加xmlns上下文,如下所示
xmlns:context="http://www.springframework.org/schema/context"
<?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">
需要包含上述链接
<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>
添加这两个模式位置。这足够而且效率高,而不是添加所有不必要的架构
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
架构位置的此路径是错误的:
http://www.springframework.org/schema/beans
正确的路径应该以/
结束:
http://www.springframework.org/schema/beans/
我遇到了问题
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依赖项并组装成一个jar,则META-INF/spring.schemas
可能会被另一个spring依赖项的spring.schemas
覆盖。
(从你装配的jar中提取该文件,你就会明白)
Spring模式只是一堆看起来像这样的行:
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附加到单个文件中。
校验:
如果包含所需XSD的jar文件未包含在已部署的类路径中,也可能导致此错误。
确保容器中的依赖项可用。
如果使用STS,您可以在Eclipse中将配置文件标记为“Bean Configuration”文件(您可以在创建或右键单击XML文件时指定):
你的项目必须有Spring Nature(例如右键单击maven项目):
然后使用Spring Config Editor默认打开spring.xml
这个编辑器有Namespaces选项卡
这使您可以指定名称空间:
请注意,这取决于依赖关系(使用maven项目),所以如果没有在maven的pom.xml中定义spring-tx
,那么选项不存在,这会阻止你有The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven' 'context:component-scan'问题......
现在为时已晚,但有些可能对其他人有用
匹配的通配符是严格的,但是找不到元素'context:component-scan的声明
这意味着您错过了一些声明或XML中未找到的必需声明
在我的情况下,我忘了添加以下内容
添加此问题后,问题就消失了
<?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">
当您在xml中首次添加上下文:component-scan时,需要添加以下内容。
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">
正确的路径不应该以“/”结尾,我错了造成了麻烦
正确的方法:
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
使用名称空间声明和模式位置,您还可以检查名称空间使用的语法,例如: -
<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 -->