【Java基础干货】Spring BeanCreationException异常总结

Spring BeanCreationException异常总结

BeanCreationException,顾名思义是Bean创建过程中抛出异常,具体有以下几种常见异常

1、org.springframework.beans.factory.NoSuchBeanDefinitionException

上下文中不存在此Bean,一般原因在没有声明Bean,BeanA尝试注入BeanB,但是spring上下文中不存在BeanB。

常见系数:☆☆☆☆☆

@Component
public class BeanA {

    @Autowired
    private BeanB beanB;
    ...
}

注意需要在以下地方检查是否声明Bean

  • XML文件中使用<bean />

  • @Bean @Configuration

  • @Component @Service @Controller @Repository

同时检查配置文件和类文件是否真的已经被Spring 装载到上下文。

 

2、org.springframework.beans.factory.NoUniqueBeanDefinitionException

当一个Bean有多个实现,例如BeanB1 和BeanB2都实现了同一个接口

常见系数:☆☆☆☆☆

@Component
public class BeanB1 implements IBeanB { ... }
@Component
public class BeanB2 implements IBeanB { ... }

@Component
public class BeanA {

    @Autowired
    private IBeanB dependency;
    ...
}

 

 

3、org.springframework.beans.BeanInstantiationException

Bean 实例化过程中出现异常,具体原因需要查看nested exception后面报错

3.1 Custom Exception

业务自定义抛出异常,如下抛出空指针异常

常见系数:☆☆

 

3.2 java.lang.InstantiationException

在xml配置中使用抽象类定义bean

常见系数:☆☆

@Component
public abstract class BeanA implements IBeanA { ... }

<bean id="beanA" class="org.baeldung.web.BeanA" />

报错日志

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'beanA' defined in class path resource [beansInXml.xml]: 
Instantiation of bean failed; 
nested exception is org.springframework.beans.BeanInstantiationException: 
Could not instantiate bean class [org.baeldung.web.BeanA]: 
Is it an abstract class?; 
nested exception is java.lang.InstantiationException

 

3.3 java.lang.NoSuchMethodException

 

如果一个bean类没有默认的构造方法,spring在创建bean实例时

常见系数:☆☆☆☆

@Component
public class BeanA implements IBeanA {

    public BeanA(final String name) {
        super();
        System.out.println(name);
    }
}

报错日志如下

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'beanA' defined in class path resource [beansInXml.xml]: 
Instantiation of bean failed; 
nested exception is org.springframework.beans.BeanInstantiationException: 
Could not instantiate bean class [org.baeldung.web.BeanA]: 
Is it an abstract class?; 
nested exception is java.lang.InstantiationException

4、org.springframework.beans.factory.CannotLoadBeanClassException

当spring加载不到bean对应的类文件时,这种异常将会被抛出。这种情况很有可能发生在当配置文件中的类路径全称找不到对应文件时。

<bean id="beanZ" class="org.baeldung.web.BeanZ" />

报错日志

nested exception is org.springframework.beans.factory.BeanCreationException: 
...
nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: 
Cannot find class [org.baeldung.web.BeanZ] for bean with name 'beanZ'
defined in class path resource [beansInXml.xml]; 
nested exception is java.lang.ClassNotFoundException: org.baeldung.web.BeanZ

 

 

5、org.springframework.beans.factory.BeanCurrentlyInCreationException

BeanCurrentlyInCreationException是BeanCreationException的一个子类,经常在发生在错误的使用构造方法注入bean。如循环依赖的情况,以下例子中循环依赖无法解决。

常见系数:☆☆☆

@Component
public class BeanA implements IBeanA {
    private IBeanB beanB;

    @Autowired
    public BeanA(final IBeanB beanB) {
        super();
        this.beanB = beanB;
    }
}
@Component
public class BeanB implements IBeanB {
    final IBeanA beanA;

    @Autowired
    public BeanB(final IBeanA beanA) {
        super();
        this.beanA = beanA;
    }
}

异常信息

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'beanA' defined in file [...BeanA.class]: 
Unsatisfied dependency expressed through constructor argument with index 0 
of type [org.baeldung.web.IBeanB]: : 
Error creating bean with name 'beanB' defined in file [...BeanB.class]: 
Unsatisfied dependency expressed through constructor argument with index 0 
of type [org.baeldung.web.IBeanA]: : 
Error creating bean with name 'beanA': Requested bean is currently in creation: 
Is there an unresolvable circular reference?; 
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: 
Error creating bean with name 'beanA': 
Requested bean is currently in creation: 
Is there an unresolvable circular reference?; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'beanB' defined in file [...BeanB.class]: 
Unsatisfied dependency expressed through constructor argument with index 0 
of type [org.baeldung.web.IBeanA]: : 
Error creating bean with name 'beanA': 
Requested bean is currently in creation: 
Is there an unresolvable circular reference?; 
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: 
Error creating bean with name 'beanA': 
Requested bean is currently in creation: Is there an unresolvable circular reference?

 

下面单例的setter注入方式可以解决循环依赖问题,spring设置的缓存机制可以帮助解决循环依赖问题

@Component
public class BeanA implements IBeanA {
  	@Autowired
    private IBeanB beanB;
}
@Component
public class BeanB implements IBeanB {
  	@Autowired
    private IBeanA beanA;

}

6、org.springframework.beans.factory.BeanIsAbstractException

BeanIsAbstractException是BeanCreationException的一个子类,下面例子中定义BeanA为抽象类

public abstract class BeanA implements IBeanA {
   ...
}

<bean id="beanA" abstract="true" class="org.baeldung.web.BeanA" />

@Configuration
public class Config {
    @Autowired
    BeanFactory beanFactory;

    @Bean
    public BeanB beanB() {
        beanFactory.getBean("beanA");
        return new BeanB();
    }
}

日志信息

org.springframework.beans.factory.BeanIsAbstractException: 
Error creating bean with name 'beanA': Bean definition is abstract

 

7、org.springframework.beans.factory.BeanCreationNotAllowedException

此异常继承了BeanCreationException ,意思是bean 工厂现在要关闭了,不再允许创建bean了。


8、org.springframework.beans.factory.UnsatisfiedDependencyException

此异常继承了BeanCreationException ,意思是bean依赖的的其他bean或者一些属性没有在bean工厂中定义。

 

9、java.lang.IllegalArgumentException

这只是nested exception报错中一种,bean 初始化阶段可能会抛出各种问题,如下就是一个定义线程池的bean初始化阶段设置线程池参数,如果满足if中条件就会报IllegalArgumentException

        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();

 

  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值