基于Xml 的IOC 容器-载入配置路径
生活随笔
收集整理的這篇文章主要介紹了
基于Xml 的IOC 容器-载入配置路径
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
AbstractRefreshableApplicationContext 中只定義了抽象的loadBeanDefinitions 方法,容器真正調(diào)用的是其子類AbstractXmlApplicationContext 對(duì)該方法的實(shí)現(xiàn),AbstractXmlApplicationContext的主要源碼如下:
loadBeanDefinitions() 方法同樣是抽象方法, 是由其子類實(shí)現(xiàn)的, 也即在AbstractXmlApplicationContext 中。
public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext {private boolean validating = true;public AbstractXmlApplicationContext() {}public AbstractXmlApplicationContext(@Nullable ApplicationContext parent) {super(parent);}public void setValidating(boolean validating) {this.validating = validating;}//實(shí)現(xiàn)父類抽象的載入Bean定義方法@Overrideprotected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {// Create a new XmlBeanDefinitionReader for the given BeanFactory.//創(chuàng)建XmlBeanDefinitionReader,即創(chuàng)建Bean讀取器,并通過(guò)回調(diào)設(shè)置到容器中去,容 器使用該讀取器讀取Bean定義資源XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);// Configure the bean definition reader with this context's// resource loading environment.//為Bean讀取器設(shè)置Spring資源加載器,AbstractXmlApplicationContext的//祖先父類AbstractApplicationContext繼承DefaultResourceLoader,因此,容器本身也是一個(gè)資源加載器beanDefinitionReader.setEnvironment(this.getEnvironment());beanDefinitionReader.setResourceLoader(this);//為Bean讀取器設(shè)置SAX xml解析器beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));// Allow a subclass to provide custom initialization of the reader,// then proceed with actually loading the bean definitions.//當(dāng)Bean讀取器讀取Bean定義的Xml資源文件時(shí),啟用Xml的校驗(yàn)機(jī)制initBeanDefinitionReader(beanDefinitionReader);//Bean讀取器真正實(shí)現(xiàn)加載的方法loadBeanDefinitions(beanDefinitionReader);}protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {reader.setValidating(this.validating);}//Xml Bean讀取器加載Bean定義資源protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {//獲取Bean定義資源的定位Resource[] configResources = getConfigResources();if (configResources != null) {//Xml Bean讀取器調(diào)用其父類AbstractBeanDefinitionReader讀取定位//的Bean定義資源reader.loadBeanDefinitions(configResources);}//如果子類中獲取的Bean定義資源定位為空,則獲取FileSystemXmlApplicationContext構(gòu)造方法中setConfigLocations方法設(shè)置的資源String[] configLocations = getConfigLocations();if (configLocations != null) {//Xml Bean讀取器調(diào)用其父類AbstractBeanDefinitionReader讀取定位//的Bean定義資源reader.loadBeanDefinitions(configLocations);}}//這里又使用了一個(gè)委托模式,調(diào)用子類的獲取Bean定義資源定位的方法//該方法在ClassPathXmlApplicationContext中進(jìn)行實(shí)現(xiàn),對(duì)于我們//舉例分析源碼的FileSystemXmlApplicationContext沒(méi)有使用該方法@Nullableprotected Resource[] getConfigResources() {return null;}}以XmlBean 讀取器的其中一種策略XmlBeanDefinitionReader 為例。XmlBeanDefinitionReader 調(diào)用其父類AbstractBeanDefinitionReader 的reader.loadBeanDefinitions()方法讀取Bean 配置資源。由于我們使用ClassPathXmlApplicationContext 作為例子分析,因此getConfigResources 的返回值為null,因此程序執(zhí)行reader.loadBeanDefinitions(configLocations)分支。
?
總結(jié)
以上是生活随笔為你收集整理的基于Xml 的IOC 容器-载入配置路径的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 基于Xml 的IOC 容器-创建容器
- 下一篇: 基于Xml 的IOC 容器-分配路径处理