spring中常见异常分析

本文深入分析了Spring框架中常见的异常问题,包括NoUniqueBeanDefinitionException、BeanCreationException和NoSuchBeanDefinitionException等,详细解释了异常产生的原因及解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring中常见异常分析

1、NoUniqueBeanDefinitionException异常(没有唯一的Bean异常)
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.qf.service.UserService' available: expected single matching bean but found 2: userServiceImpl,userServiceImpl2

异常分析:主要是在获取对象时得到多个对象,造成程序不知道执行哪个对象,遇到这样的问题请检查你的获取方式即getBean(“请使用id进行获取”),不要使用反射的方式进行获取,因为通过接口反射可能得到多个子类

2、BeanCreationException异常(Bean创建异常)
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path 
resource [spring-config.xml]: Initialization of bean failed; nested exception is 
org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' 
to required type 'java.util.Date' for property 'date'; nested exception is java.lang.IllegalStateException: Cannot convert 
value of type 'java.lang.String' to required type 'java.util.Date' for property 'date': no matching editors or conversion 
strategy found

异常分析:出现Bean创建异常时,请先看下是哪个属性出现了这个异常,一般情况下这个异常是在spring-config.xml文件中对bean中的属性进行依赖注入值(即DI)时,类型不匹配造成的,故请检查类型匹配问题,值得注意的是Date类型注入时的格式为“yyyy/MM/dd”

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path 
resource [spring-config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.qf.pojo.User]: No default constructor 
found; nested exception is java.lang.NoSuchMethodException: com.qf.pojo.User.<init>()
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path 
resource [spring-config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.qf.pojo.User]: No default constructor 
found; nested exception is java.lang.NoSuchMethodException: com.qf.pojo.User.<init>()

异常分析:出现Bean创建异常时,使用set方法进行属性注入时,类中必须要有一个无参构造方法,不然也会报错

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path 
resource [spring-config.xml]: Error setting property values; nested exception is 
org.springframework.beans.NotWritablePropertyException: Invalid property 'name' of bean class [com.qf.pojo.User]: 
Bean property 'name' is not writable or has an invalid setter method. Does the parameter type of the setter match the 
return type of the getter?

异常分析:出现Bean创建异常时,使用set方法进行属性注入时,类中的属性要有set方法,不然也会报错###

3、NoSuchBeanDefinitionException(没有此类的定义异常)
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [spring-config.xml]: Cannot resolve reference to bean 'work1' while setting bean property 'work'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'work1' available

异常分析:出现Bean创建异常时,可能是你在引用bean时ref中的id写错了,这个一般spring-config.xml会报红

4、NoSuchBeanDefinitionException

在添加aop时,可能在使用spring容器获取实体类(即getBean(“id号”))失败,报没有这样的bean被定义
遇到这样的问题看看是不是没有定义这样的id,如果存在还报错误那就需要添加以下代码到spring.xml配置中
<aop:aspectj-autoproxy proxy-target-class=“true”/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值