《step5:tiny-spring-ioc学习五》——ApplicationContext登场

首先可以看一下咱们在前4篇博客的基础上初始化bean并获取bean对象的代码:

//1.读取配置
XmlBeanDefinitionReaderxmlBeanDefinitionReader=newXmlBeanDefinitionReader(newResourceLoader());
xmlBeanDefinitionReader.loadBeanDefinitions("tinyioc.xml");

//2.初始化BeanFactory并注册bean
AbstractBeanFactorybeanFactory=newAutowireCapableBeanFactory();
for(Map.Entry<String,BeanDefinition>beanDefinitionEntry:xmlBeanDefinitionReader.getRegistry().entrySet()){
beanFactory.registerBeanDefinition(beanDefinitionEntry.getKey(),beanDefinitionEntry.getValue());
}

//3.获取bean
HelloWorldServicehelloWorldService=(HelloWorldService)beanFactory.getBean("helloWorldService");
helloWorldService.helloWorld();

简单总结一下就是通过读取xml配置文件,完成bean类的初始化工作,然后再获取bean对象的方法等。
在现在的开发过程中,好像很少看到这样的代码对不对?在没有spirng注解和set注入bean的情况下,我们获取bean好像都是这么获取的:

ApplicationContextapplicationContext=newClassPathXmlApplicationContext("tinyioc.xml");
HelloWorldServicehelloWorldService=(HelloWorldService)applicationContext.getBean("helloWorldService");
helloWorldService.helloWorld();

现在对比两段代码发现区别了吧!现在看第二段代码是不是特别熟悉?

下边就是ioc引入application的过程。

于是我们引入熟悉的ApplicationContext接口,并在AbstractApplicationContext的refresh()方法中进行bean的初始化工作。

  1. 类图
    这里写图片描述
  2. 时序图
    这里写图片描述
  3. 关键代码(在前一篇博客的基础之上)
    3·1 接口ApplicationContext:
package us.codecraft.tinyioc.context;

import us.codecraft.tinyioc.beans.factory.BeanFactory;

/**
 * @author 
 */
public interface ApplicationContext extends BeanFactory {
}
3·2抽象类AbstractApplicationContext:
    package us.codecraft.tinyioc.context;

import us.codecraft.tinyioc.beans.factory.AbstractBeanFactory;

/**
 * @author yihua.huang@dianping.com
 */
public abstract class AbstractApplicationContext implements ApplicationContext {
    protected AbstractBeanFactory beanFactory;

    public AbstractApplicationContext(AbstractBeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    public void refresh() throws Exception{
    }

    @Override
    public Object getBean(String name) throws Exception {
        return beanFactory.getBean(name);
    }
}
3·3实现类ClassPathXmlApplicationContext:
package us.codecraft.tinyioc.context;

import us.codecraft.tinyioc.beans.BeanDefinition;
import us.codecraft.tinyioc.beans.factory.AbstractBeanFactory;
import us.codecraft.tinyioc.beans.factory.AutowireCapableBeanFactory;
import us.codecraft.tinyioc.beans.io.ResourceLoader;
import us.codecraft.tinyioc.beans.xml.XmlBeanDefinitionReader;

import java.util.Map;

/**
 * @author yihua.huang@dianping.com
 */
public class ClassPathXmlApplicationContext extends AbstractApplicationContext {

    private String configLocation;

    public ClassPathXmlApplicationContext(String configLocation) throws Exception {
        this(configLocation, new AutowireCapableBeanFactory());
    }

    public ClassPathXmlApplicationContext(String configLocation, AbstractBeanFactory beanFactory) throws Exception {
        super(beanFactory);
        this.configLocation = configLocation;
        refresh();
    }

    @Override
    public void refresh() throws Exception {
        XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(new ResourceLoader());
        xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
        for (Map.Entry<String, BeanDefinition> beanDefinitionEntry : xmlBeanDefinitionReader.getRegistry().entrySet()) {
            beanFactory.registerBeanDefinition(beanDefinitionEntry.getKey(), beanDefinitionEntry.getValue());
        }
    }

}

3·4 测试类ApplicationContexxtTest:

package us.codecraft.tinyioc.context;

import org.junit.Test;
import us.codecraft.tinyioc.HelloWorldService;

/**
 * @author yihua.huang@dianping.com
 */
public class ApplicationContextTest {

    @Test
    public void test() throws Exception {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("tinyioc.xml");
        HelloWorldService helloWorldService = (HelloWorldService) applicationContext.getBean("helloWorldService");
        helloWorldService.helloWorld();
    }
}

以上是该部分内容的关键代码,完整代码请点击《下载

  1. 总结
    至此为止,我们的的tiny-spring的IoC部分可说完工了。这部分的类、方法命名和作用,都是对应Spring中相应的组件。虽然代码量只有400多行,但是已经有了基本的IoC功能!
    自总:
    这个学习tiny-spring的过程中,对于spring的ioc容器有了进一步的了解。接下来,便是tiny-spring和spring的真实映射了。包括xml配置到注解的发展变化等。## 标题 ##
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值