Spring依赖查找中的常见异常

39次阅读

共计 4223 个字符,预计需要花费 11 分钟才能阅读完成。

[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

正文完
 0