Spring源码学习--ClassPathXmlApplicationContext+FileSystemXmlApplicationContext+XmlWebApplicationContext

文章来源:

1 https://blog.csdn.net/qq924862077/article/details/59090521
2 https://blog.csdn.net/qq924862077/article/details/58653218
3 https://blog.csdn.net/qq924862077/article/details/58650318

这里写图片描述

一 ClassPathXmlApplicationContext

在实现类ClassPathXmlApplicationContext中其实并没有多少重要的操作,主要是在构造函数中配置Spring配置文件的路径:

public class DaoOperationMain {  

    public static void main(String[] args) {  
        ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");  
    }  
}  

具体的applicationContext.xml解析相关的操作都在父类refresh中进行操作。

/** 
 * 并没有太多具体的操作,主要是初始化构造函数,主要的操作都在父类中 
 */  
public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {  

    private Resource[] configResources;  

    public ClassPathXmlApplicationContext() {  
    }  
    public ClassPathXmlApplicationContext(ApplicationContext parent) {  
        super(parent);  
    }  
    public ClassPathXmlApplicationContext(String configLocation) throws BeansException {  
        this(new String[] {configLocation}, true, null);  
    }  
    public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {  
        this(configLocations, true, null);  
    }  
    public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {  
        this(configLocations, true, parent);  
    }  
    public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {  
        this(configLocations, refresh, null);  
    }  
    public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)  
            throws BeansException {  

        super(parent);  
        //设置spring的配置文件  
        setConfigLocations(configLocations);  
        if (refresh) {  
            //调用父类的refresh函数,进行一系列初始化  
            refresh();  
        }  
    }  



    public ClassPathXmlApplicationContext(String path, Class<?> clazz) throws BeansException {  
        this(new String[] {path}, clazz);  
    }  


    public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz) throws BeansException {  
        this(paths, clazz, null);  
    }  


    public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, ApplicationContext parent)  
            throws BeansException {  

        super(parent);  
        Assert.notNull(paths, "Path array must not be null");  
        Assert.notNull(clazz, "Class argument must not be null");  
        this.configResources = new Resource[paths.length];  
        for (int i = 0; i < paths.length; i++) {  
            this.configResources[i] = new ClassPathResource(paths[i], clazz);  
        }  
        //调用父类的refresh函数,进行一系列初始化  
        refresh();  
    }  


    @Override  
    protected Resource[] getConfigResources() {  
        return this.configResources;  
    }  

}  

二 FileSystemXmlApplicationContext

FileSystemXmlApplicationContext的源码如下:

/** 
 * 并没有太多具体的操作,很多初始化操作都在其父类中 
 */  
public class FileSystemXmlApplicationContext extends AbstractXmlApplicationContext {  

    public FileSystemXmlApplicationContext() {  
    }  

    public FileSystemXmlApplicationContext(ApplicationContext parent) {  
        super(parent);  
    }  
    public FileSystemXmlApplicationContext(String configLocation) throws BeansException {  
        this(new String[] {configLocation}, true, null);  
    }  
    public FileSystemXmlApplicationContext(String... configLocations) throws BeansException {  
        this(configLocations, true, null);  
    }  
    public FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {  
        this(configLocations, true, parent);  
    }  
    public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {  
        this(configLocations, refresh, null);  
    }  
    public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)  
            throws BeansException {  

        //调用父类的构造函数  
        super(parent);  
        //设置配置文件的地址,如application.xml  
        setConfigLocations(configLocations);  
        if (refresh) {  
            //刷新容器,其实就是重新初始化容器  
            refresh();  
        }  
    }  
    @Override  
    protected Resource getResourceByPath(String path) {  
        if (path != null && path.startsWith("/")) {  
            path = path.substring(1);  
        }  
        return new FileSystemResource(path);  
    }  
}  

三 XmlWebApplicationContext

XmlWebApplicationContext是用于web容器的应用上下文的,其也没有太多操作,主要还是applicationContext.xml的解析操作,完整源码如下:

public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {  

    /** Default config location for the root context */  
    //默认配置文件地址  
    public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";  

    /** Default prefix for building a config location for a namespace */  
    public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";  

    /** Default suffix for building a config location for a namespace */  
    public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";  

    //解析applicationContext.xml  
    @Override  
    protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, 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.setEnvironment(getEnvironment());  
        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);  
    }  

    protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {  
    }  

    protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {  
        String[] configLocations = getConfigLocations();  
        if (configLocations != null) {  
            for (String configLocation : configLocations) {  
                reader.loadBeanDefinitions(configLocation);  
            }  
        }  
    }  

    //获取默认配置文件地址  
    @Override  
    protected String[] getDefaultConfigLocations() {  
        if (getNamespace() != null) {  
            return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};  
        }  
        else {  
            return new String[] {DEFAULT_CONFIG_LOCATION};  
        }  
    }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值