美文网首页
Spring源码解析(一)-BeanFactory和Factor

Spring源码解析(一)-BeanFactory和Factor

作者: Jerry_dong | 来源:发表于2017-06-21 21:58 被阅读0次

    本系列博客针对Spring4.2.4版本,参考《Spring技术内幕》第2版

    BeanFactory

    BeanFactory是最基本的IOC容器接口,这个接口为具体的IOC容器的实现作了最基本的功能规定

    BeanFactory类结构体系

    BeanFactory结构体系.png

    BeanFactory类详解

    public interface BeanFactory {
    
        //这里是对FactoryBean的转义定义,因为如果使用bean的名字检索    FactoryBean得到的对象是工厂生成的对象,  
        //如果需要得到工厂本身,加上“&”
        String FACTORY_BEAN_PREFIX = "&";
    
        //根据bean的名字,在IOC容器中得到bean实例
        Object getBean(String name) throws BeansException;
    
        //根据bean的名字和Class类型来得到bean实例,若根据名字取得的bean实例的Class类型和需要的不同的话
        //会抛出异常
        <T> T getBean(String name, Class<T> requiredType) throws BeansException;
    
        //根据指定的类型取得IoC容器中管理的Bean
        <T> T getBean(Class<T> requiredType) throws BeansException;
    
        //重载了getBean(String name)方法,可变参数用来指定构造方法或者工厂方法需要的参数
        //覆盖之前已经在bean中定义好的参数
        Object getBean(String name, Object... args) throws BeansException;
    
        //重载了getBean(Class<T> requiredType)方法,可变参数用来指定构造方法或者工厂方法需要的参数
        //覆盖之前已经在bean中定义好的参数
        <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
    
        //判断在IOC容器是否 有这个名字的bean
        boolean containsBean(String name);
    
        //查询指定名字的Bean是否是单态类型(singleton)
        boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
    
        //查询指定名字的Bean是否是原型(prototype)类型
        boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
    
        //查询指定名字的Bean的class类型是否支持特定的class类型
        boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;
    
        //查询指定名字的Bean的class类型
        Class<?> getType(String name) throws NoSuchBeanDefinitionException;
    
        //查询指定名字的Bean的所有别名
        String[] getAliases(String name);
    
    }
    

    生命周期

     * <p>Bean factory implementations should support the standard bean lifecycle interfaces
     * as far as possible. The full set of initialization methods and their standard order is:<br>
     * 1. BeanNameAware's {@code setBeanName}<br>
     * 2. BeanClassLoaderAware's {@code setBeanClassLoader}<br>
     * 3. BeanFactoryAware's {@code setBeanFactory}<br>
     * 4. ResourceLoaderAware's {@code setResourceLoader}
     * (only applicable when running in an application context)<br>
     * 5. ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
     * (only applicable when running in an application context)<br>
     * 6. MessageSourceAware's {@code setMessageSource}
     * (only applicable when running in an application context)<br>
     * 7. ApplicationContextAware's {@code setApplicationContext}
     * (only applicable when running in an application context)<br>
     * 8. ServletContextAware's {@code setServletContext}
     * (only applicable when running in a web application context)<br>
     * 9. {@code postProcessBeforeInitialization} methods of BeanPostProcessors<br>
     * 10. InitializingBean's {@code afterPropertiesSet}<br>
     * 11. a custom init-method definition<br>
     * 12. {@code postProcessAfterInitialization} methods of BeanPostProcessors
     *
     * <p>On shutdown of a bean factory, the following lifecycle methods apply:<br>
     * 1. DisposableBean's {@code destroy}<br>
     * 2. a custom destroy-method definition
    

    BeanFactory和FactoryBean的区别和联系

    BeanFactory是spring容器的根容器,描述了子接口(子容器)的一些处理原则,包括一些生命周期,方法和约定。FactoryBean本身是存活在IOC容器(BeanFactory)中,本身也是一个工厂,用于创建由FactoryBean所能创建出来的对象,再创建出该对象时,可以对该对象的后面或者前面做一些额外的操作来实现AOP。

    相关文章

      网友评论

          本文标题:Spring源码解析(一)-BeanFactory和Factor

          本文链接:https://www.haomeiwen.com/subject/ssuzfxtx.html