Spring源码学习笔记
?最近在看spring的源码,担心忘掉了,打个记号,也请大家一起指正其中的错误,防止走歪路。
从xml配置文件加载入手
xml配置文件加载由
org.springframework.context.support.ClassPathXmlApplicationContext完成,该类的继承关系如下:
?实际调用:
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);}
?
?