spring启动报错,找不到‘beans’的声明

 

一个项目启动报错

org.xml.sax.SAXParseException; lineNumber: 19; columnNumber: 10; cvc-elt.1:找不到元素 'beans' 的声明。org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Line 19 in XML document from ServletContext resource [/WEB-INF/classes/applicationContext.xml]
is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber:
19; columnNumber: 10; cvc-elt.1: 找不到元素 'beans' 的声明。at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)

之所以报这个错,是因为项目里spring的jar包版本和spring配置文件里声明的xsd版本不一致,声明是3.2,但jar包里是4.0

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	
	xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
      ">
</beans>

Spring默认在启动时是要加载XSD文件来验证xml文件的,所以如果有的时候断网了,或者一些开源软件切换域名,那么就很容易碰到应用启动不了。为了防止这种情况,Spring提供了一种机制,默认从本地加载XSD文件。
写代码,一些配置文件难免要复制粘贴,但不应该只是复制粘贴就完事了,要检查相关代码是否符合自己项目的情况。

参考:https://blog.csdn.net/xhaimail/article/details/79886478

`org.springframework.beans.factory.beanDefinitionStoreException: failed to read candidate component class` 这个错误通常是由于Spring框架在启动时无法加载指定的Bean定义类所导致的。这可能由以下几个原因造成: 1. 类路径问题:可能是Spring试图加载的类不在类路径(Classpath)中,或者类路径配置有误。 2. Bean类问题:被Spring扫描的类可能存在语法错误或者编译错误,导致无法加载。 3. 扫描注解问题:可能由于`@Component`, `@Service`, `@Repository`, `@Controller`等注解使用不当,例如注解未被正确扫描到或者重复扫描,或者类上缺少相应的注解。 4. Bean定义冲突:可能存在多个相同名称的Bean定义在不同的类中,导致Spring框架无法决定使用哪一个。 5. XML配置错误:如果使用的是XML配置方式,可能存在配置文件的格式错误或配置项不正确。 6. 序列化问题:如果类需要被序列化,但未实现`Serializable`接口,也会导致此类错误。 解决这个问题通常需要以下几个步骤: - 确认类路径配置正确,确保Spring试图加载的类存在于类路径中。 - 检查Bean定义类是否有语法错误或编译问题,如果有,进行修正。 - 确保使用了`@Component`, `@Service`, `@Repository`, `@Controller`等注解的类被Spring扫描到,可以通过调整`@ComponentScan`或`@SpringBootApplication`注解的参数来指定扫描路径。 - 如果有多个相同名称的Bean定义,需要检查并解决冲突。 - 如果是基于XML的配置,检查XML文件的正确性。 - 确保需要被Spring管理的类实现了`Serializable`接口,如果需要的话。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值