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

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » Spring依赖查找中的常见异常

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址