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”/>