Spring-ClassPathXmlApplicationContext源码探讨-解析XML文件

本文深入探讨了Spring中ClassPathXmlApplicationContext如何解析XML配置文件的过程。从构造方法出发,详细跟踪了从refresh()到bean定义注册的完整流程,涉及抽象类及接口如AbstractApplicationContext、AbstractXmlApplicationContext和BeanDefinitionDocumentReader等。解析过程包括parseDefaultElement处理标准标签,以及parseCustomElement处理自定义标签。解析过程中还解释了命名空间处理器的加载和作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

使用ClassPathXmlApplicationContext作为Spring容器,可以在构造方法添加XML文件路径,创建Spring容器,将会解析XML文件的配置信息,下面看看解析XML流程。
Spring版本:5.0.6

实例

最原始的方式,创建Spring容器,代码:

/**
 * @author wzx
 * @time 2018/7/29
 */
public class SpringTest {
   

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        Person person = context.getBean("person", Person.class);
        System.out.println(person.getName());
    }
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />

    <bean name="person" class="cn.wzx.model.Person">
        <property name="name" value="wzx"/>
    </bean>

    <bean name="dog" class="cn.wzx.model.Dog">
        <property name="name" value="niba"/>
    </bean>

</beans>
时序图

解析XML文件的主要流程:

ClassPathXmlApplicationContext构造方法最终会调用父类AbstractApplicationContext的refresh()方法,refresh()方法是创建Spring容器的入口,是核心方法,源码:

@Override
public void refresh() throws BeansException, IllegalStateException {
    synchronized (this.startupShutdownMonitor) {
        // Prepare this context for refreshing.
        prepareRefresh();
        // 通过子类刷新bean factory.
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
        // Prepare the bean factory for use in this context.
        prepareBeanFactory(beanFactory);
        try {
            // Allows post-processing of the bean factory in context subclasses.
            postProcessBeanFactory(beanFactory);
            // Invoke factory processors registered as beans in the context.
            invokeBeanFactoryPostProcessors(beanFactory);
            // Register bean processors that intercept bean creation.
            registerBeanPostProcessors(beanFactory);
            // Initialize message source for this context.
            initMessageSource();
            // Initialize event multicaster for this context.
            initApplicationEventMulticaster();
            // Initialize other special beans in specific context subclasses.
            onRefresh();
            // Check for listener beans and register them.
            registerListeners();
            // Instantiate all remaining (non-lazy-init) singletons.
            finishBeanFactoryInitialization(beanFactory);
            // Last step: publish corresponding event.
            finishRefresh();
        }
        catch (BeansException ex) {
            if (logger.isWarnEnabled()) {
                logger.warn("Exception encountered during context initialization - " +
                        "cancelling refresh attempt: " + ex);
            }
            // Destroy already created singletons to avoid dangling resources.
            destroyBeans();
        
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值