Spring配置文件报错cvc-elt.1.a:无法找到‘beans’元素声明求解决
解决Spring配置文件的cvc-elt.1.a错误
问题出在XML命名空间的配置上:你把Spring beans的命名空间定义成了带前缀的beans:,但根元素<beans>没有使用这个前缀,同时也没指定默认命名空间,导致XML解析器找不到该元素的Schema声明。
直接修改命名空间配置,把带前缀的xmlns:beans改成默认命名空间xmlns即可,修正后的完整配置如下:
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="testBean" class="alishev.spring.demo.TestBean"> <constructor-arg value="Neil"/> </bean> </beans>
xmlns="http://www.springframework.org/schema/beans"将该地址设为默认命名空间,所有不带前缀的元素(比如<beans>、<bean>)都会归属到这个命名空间,和xsi:schemaLocation里的对应地址匹配后,解析器就能找到对应的Schema定义。- 你之前改成
beans:beans没用,是因为即使加了前缀,默认命名空间仍缺失,而且这种写法完全没必要——Spring配置文件通常都会把beans设为默认命名空间来简化写法。
内容的提问来源于stack exchange,提问作者Егор Чистяков
相关产品推荐
相关产品推荐

