读书人

Spring源码学习札记

发布时间: 2013-10-22 16:17:14 作者: rapoo

Spring源码学习笔记

?最近在看spring的源码,担心忘掉了,打个记号,也请大家一起指正其中的错误,防止走歪路。

从xml配置文件加载入手

xml配置文件加载由

org.springframework.context.support.ClassPathXmlApplicationContext完成,该类的继承关系如下:


Spring源码学习札记
?实际调用:

AbstractApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});PersonService service = (PersonService)context.getBean("personService");

?

?Beans.xml加载在创建ClassPathXmlApplicationContext实例时完成,上面调用方法仅仅是ClassPathXmlApplicationContext构造函数之一。

? 该构造函数调用具体内部步骤如下:

DefaultListableBeanFactory beanFactory = createBeanFactory();customizeBeanFactory(beanFactory);

加载bean定义的xml文件

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws 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.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);}

?

?

读书人网 >软件架构设计

热点推荐