spring

spring通过加载配置文件的方式来装载初始化bean。以下面的使用为例:

ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:performer.xml");
Performer performer = (Performer) ctx.getBean("duke");
performer.perform();

主要使用到FileSystemXmlApplicationContext,该类的继承结构如下:

类继承结构

AbrstactApplicationContext类做为基本抽象类,实现了大部分方法并且提供了程序基本的运行框架。
在运行过程中,该类的refresh方法会调用obtainFreshBeanFactory方法。通过一步步程序运行定位到
子类AbstractRefreshableApplicationContext中的refreshBeanFactory()方法。
protected final void refreshBeanFactory() throws BeansException {

...
DefaultListableBeanFactory beanFactory = createBeanFactory();//初始化beanFactory
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
loadBeanDefinitions(beanFactory);//装载 bean的定义文件
  ...

其中loadBeanDefinitions具体是由子类AbstractXmlApplicationContext实现

1 protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
2 // Create a new XmlBeanDefinitionReader for the given BeanFactory.
3 XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

// Configure the bean definition reader with this context's
// resource loading environment.
4 beanDefinitionReader.setEnvironment(this.getEnvironment());
5 beanDefinitionReader.setResourceLoader(this);
6 beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
7 initBeanDefinitionReader(beanDefinitionReader);
8 loadBeanDefinitions(beanDefinitionReader);
    }

跟踪代码到 类XmlBeanDefinitionReader中 (此时已经获取到配置文件的inputStream流)
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值