目录

spring- BeanFactory与ApplicationContext

李羽秋
李羽秋 2023年03月15日  ·  阅读 1,825

spring- BeanFactory与ApplicationContext

1. 概述

我们常常将BeanFactory称为容器,将ApplicationContext称为上下文,一直以来,人们认为者两者是同一个事物,我们来探究一下他们之间的关系。接下来来看一个例子

2. 例子说明

配置类

package org.liyuqiu.spring.ioc.test;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("org.liyuqiu.spring.ioc.test")
public class ContainerConfig {

}

获取信息

package org.liyuqiu.spring.ioc.test;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringIoc implements BeanFactoryAware, ApplicationContextAware {
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println(System.identityHashCode(beanFactory));
        System.out.println(beanFactory.getClass().getName());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println(System.identityHashCode(applicationContext));
        System.out.println(applicationContext.getClass().getName());
    }
}

启动类

package org.liyuqiu.spring.ioc.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainContainer {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ContainerConfig.class);
        context.close();
    }

}

结果

image-20220718111405574

我们可以看到两者不仅不是一个对象,连类型都不一样。我们来看一下Spring官方如何论述二者的关系:

简而言之,BeanFactory 提供了配置框架和基本功能,ApplicationContext增加了更多针对企业的功能,ApplicationContext是BeanFactory的一个完整的超集。

按照官方的解释:二者是一个包含与被包含的关系,那么在ApplicationContext中我们可以获得根容器吗?

当然我们可以调用getAutowireCapableBeanFactory即可。

@Component
public class SpringIoc implements BeanFactoryAware, ApplicationContextAware {
    private BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
        System.out.println(System.identityHashCode(beanFactory));
        System.out.println(beanFactory.getClass().getName());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
        System.out.println(System.identityHashCode(factory.hashCode()));
        System.out.println(factory.getClass().getName());
        System.out.println("两者相等吗:" + (factory == beanFactory));

    }
}
@Component
public class SpringIoc implements BeanFactoryAware, ApplicationContextAware {
    private BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
        System.out.println(System.identityHashCode(beanFactory));
        System.out.println(beanFactory.getClass().getName());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
        System.out.println(System.identityHashCode(factory.hashCode()));
        System.out.println(factory.getClass().getName());
        System.out.println("两者相等吗:" + (factory == beanFactory));

    }
}

@Component
public class SpringIoc implements BeanFactoryAware, ApplicationContextAware {
    private BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
        System.out.println(System.identityHashCode(beanFactory));
        System.out.println(beanFactory.getClass().getName());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
        System.out.println(System.identityHashCode(factory.hashCode()));
        System.out.println(factory.getClass().getName());
        System.out.println("两者相等吗:" + (factory == beanFactory));

    }
}

结果

image-20220718112804924

3. 两者差异

BeanFactory 接口提供了一种高级配置机制,能够管理任何类型的对象,ApplicationContext是其子接口,增加了以下特性:

  • 与Spring的AOP功能轻松集成
  • 消息资源处理
  • 活动发布
  • 应用层特定的上下文

参考:https://juejin.cn/post/6844904061574053901

分类: Spring
标签: