美文网首页
ApplicationContext如何加载Bean

ApplicationContext如何加载Bean

作者: 西普士 | 来源:发表于2017-08-28 17:29 被阅读302次

名词

  • Bean
    java类的代理。在Spring中,那些组成你应用程序的主体(backbone)及由Spring IoC容器所管理的对象,被称之为bean。 简单地讲,bean就是由Spring容器初始化、装配及管理的对象,除此之外,bean就与应用程序中的其他对象没有什么区别了。

  • BeanFactory
    spring ioc的容器,负责对Bean进行管理

  • ApplicationContext
    BeanFactory功能的增强

  • IOC
    传统上由程序代码直接操控的对象的调用权交给容器,现在通过容器来实现对象组件的装配和管理。所谓的“控制反转”概念就是对组件对象控制权的转移,从程序代码本身转移到了外部容器。

Demo

applicationContext 类图 applicationContext 序列图

测试代码ClassPathXmlApplicationContextTests.testAliasThatOverridesParent

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(FQ_SIMPLE_CONTEXT);
Object someMs = ctx.getBean("someMessageSource");

ClassPathXmlApplicationContext.java, 用于管理xml描述的bean

public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {
    public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
      super(parent);
      setConfigLocations(configLocations);
      if (refresh) {
        refresh();
      }
  }
}

AbstractApplicationContext.java, bean容器的基类

public abstract class AbstractApplicationContext extends DefaultResourceLoader
        implements ConfigurableApplicationContext {
    @Override
    public void refresh() throws BeansException, IllegalStateException {
      synchronized (this.startupShutdownMonitor) {
        // Prepare this context for refreshing.
        prepareRefresh();

        // Tell the subclass to refresh the internal bean factory.
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
        ......
    }

    protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;

    @Override
    public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;
}

AbstractRefreshableApplicationContext.java

@Override
protected final void refreshBeanFactory() throws BeansException {
  if (hasBeanFactory()) {
    destroyBeans();
    closeBeanFactory();
  }
  try {
    DefaultListableBeanFactory beanFactory = createBeanFactory();
    beanFactory.setSerializationId(getId());
    customizeBeanFactory(beanFactory);
    loadBeanDefinitions(beanFactory);
    synchronized (this.beanFactoryMonitor) {
      this.beanFactory = beanFactory;
    }
  }
  catch (IOException ex) {
    throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
  }
}

AbstractXmlApplicationContext.java

public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext {
      @Override
    protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
        // Create a new XmlBeanDefinitionReader for the given BeanFactory.
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

        // Configure the bean definition reader with this context's
        // resource loading environment.
        beanDefinitionReader.setEnvironment(this.getEnvironment());
        beanDefinitionReader.setResourceLoader(this);
        beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

        // Allow a subclass to provide custom initialization of the reader,
        // then proceed with actually loading the bean definitions.
        initBeanDefinitionReader(beanDefinitionReader);
        loadBeanDefinitions(beanDefinitionReader);
    }
}

相关文章

网友评论

      本文标题:ApplicationContext如何加载Bean

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