Java基础干货Spring BeanCreationException异常总结

Posted 在路上的德尔菲

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();

 

以上是关于Java基础干货Spring BeanCreationException异常总结的主要内容,如果未能解决你的问题,请参考以下文章

程序员面试备战篇:69个经典Spring面试专题解析(干货分享答案)

Java基础干货如何优雅关闭线程池实践总结

Spring基础学习,没有理论 都是干货

不可多得的干货!docker启动nginx

不可多得的干货!docker启动nginx

干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结