转载

Spring依赖查找中的常见异常

[TOC]

BeansException的子接口

异常类型 触发条件(举例) 场景举例
NoSuchBeanDefinitionException 当查找 Bean 不存在于 IoC 容器时 BeanFactory#getBean ObjectFactory#getObject
NoUniqueBeanDefinitionException 类型依赖查找时,IoC 容器存在多 个 Bean 实例 BeanFactory#getBean(Class)
BeanInstantiationException 当 Bean 所对应的类型非具体类时 BeanFactory#getBean
BeanCreationException 当 Bean 初始化过程中 Bean 初始化方法执行异常 时
BeanDefinitionStoreException 当 BeanDefinition 配置元信息非法 时 XML 配置资源无法打开时

Rumenz.java实体类

package com.rumenz;

import org.springframework.beans.factory.InitializingBean;

public class Rumenz implements InitializingBean {

    private Integer id;
    private String name;


    @Override
    public void afterPropertiesSet() throws Exception {
        throw new Exception("初始化异常。。。。");
    }

    @Override
    public String toString() {
        return "Rumenz{" +
                "id=" + id +
                ", name='" + name + '/'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

NoSuchBeanDefinitionException不存在要查找的Bean

package com.rumenz;
import java.util.Map;
public class DemoApplication {

    public static void main(String[] args) {
         AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
         ac.register(DemoApplication.class); //不注册Rumenz
         ac.refresh();

         //NoSuchBeanDefinitionException不存在要查找的Bean
         byBeanFactory(ac);
         ac.close();
    }
     //NoSuchBeanDefinitionException不存在要查找的Bean
    private static void byBeanFactory(AnnotationConfigApplicationContext ac) {
        ac.getBean(Rumenz.class);
    }

}

输出

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rumenz.Rumenz' available

NoUniqueBeanDefinitionException 容器中存在多个同类型的Bean

配置Bean的

注册了两个Rumenz

package com.rumenz;
import org.springframework.context.annotation.Bean;

public class Config {
    @Bean
    public Rumenz rumenz1(){
        Rumenz r=new Rumenz();
        r.setId(456);
        r.setName("入门小站");
        return r;
    }

    @Bean
    public Rumenz rumenz(){
        Rumenz r=new Rumenz();
        r.setId(123);
        r.setName("入门小站");
        return r;
    }


}

案例

package com.rumenz;



import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;


public class DemoApplication {

    public static void main(String[] args) {
         AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
         ac.register(Config.class); //注册Rumenz
         ac.refresh();

         // NoUniqueBeanDefinitionException要查找的Bean不唯一
         byBeanFactory(ac);
         ac.close();
    }
     // NoUniqueBeanDefinitionException要查找的Bean不唯一
    private static void byBeanFactory(AnnotationConfigApplicationContext ac) {
        ac.getBean(Rumenz.class);
    }

}

输出

Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.rumenz.Rumenz' available: expected single matching bean but found 2: rumenz1,rumenz

BeanInstantiationException不能被实例化

package com.rumenz;


public class DemoApplication {

    public static void main(String[] args) {
         AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
         BeanDefinitionBuilder bb=BeanDefinitionBuilder.genericBeanDefinition(BeanFactory.class);
         ac.registerBeanDefinition("err",bb.getBeanDefinition());
         ac.refresh();
         ac.close();
    }
}

输出

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.beans.factory.BeanFactory]: Specified class is an interface

BeanCreationException Bean初始化异常

package com.rumenz;



import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;


public class DemoApplication {

    public static void main(String[] args) {
         AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
         // 4.BeanCreationException Bean初始化异常
         BeanDefinitionBuilder bb=BeanDefinitionBuilder.genericBeanDefinition(Rumenz.class);
         ac.registerBeanDefinition("err",bb.getBeanDefinition());
         ac.refresh();
         ac.close();
    }

}

BeanDefinitionStoreException 配置元错误

Beanss.xml 不存在就会触发BeanDefinitionStoreException

ApplicationContext acc=new ClassPathXmlApplicationContext("Beanss.xml");

输出

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Beanss.xml]; nested exception is java.io.FileNotFoundException: class path resource [Beanss.xml] cannot be opened because it does not exist

Spring依赖查找中的常见异常

原文  https://segmentfault.com/a/1190000023196358
正文到此结束
Loading...