上一篇Spring源码系列-2-整体debug,我们整体过了一遍Spring的启动过程,这篇我们来看一下Spring启动的部分细节
解析Spring配置文件${}占位符细节
调试样例
我们启动类中传递的配置文件名和上一篇样例不同,带了占位符的名称spring-${username}.xml
,同时我们存在一个配置文件spring-dell.xml
。
// 启动类
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-${username}.xml");
Student student = context.getBean(Student.class);
System.out.println("姓名 " + student.getName());
}
// 配置文件为spring-dell.xml
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean class="com.mashibing.Student" id="student">
<property name="id" value="1"></property>
<property name="name" value="zhangsan"></property>
</bean>
</beans>
解析过程
ClassPathXmlApplicationContext
构造方法细节
-
启动调试,调用
ClassPathXmlApplicationContext
构造方法 -
看一下
super(parent)
做了什么,super指的是AbstractApplicationContext
public AbstractApplicationContext(@Nullable ApplicationContext parent) { this(); setParent(parent); }
简单看下有哪些属性,上一篇整体看
refresh()
函数的时候,基本都见过这些属性,所以了解下定义了哪些属性,后续看到就不要蒙了。
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
public static final String MESSAGE_SOURCE_BEAN_NAME = "messageSource";
/**
* 创建上下文的唯一标识
*
* Unique id for this context, if any. */
private String id = ObjectUtils.identityToString(this);
/** Environment used by this context. */
@Nullable
private ConfigurableEnvironment environment;
/**
* 用来存放beanFactory的后置处理器集合
* BeanFactoryPostProcessors to apply on refresh. */
private</