原创不易,搬砖请注明出处,否则版权必究.
个人公众号: 码农脱贫
大纲:
1.入门需要的所有demo代码
2.先看一个不是很重要的super方法,初步感受一下源码的魅力
重点:super(parent)方法
1.准备工作
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myTestBean" class="com.spring.config.entity.MyTestBean" >
<property name="id" value="123456"/>
</bean>
</beans>
MyTestBean
package com.spring.config.entity;
/**
* @author 1
* @version 1.0
* @description: TODO
* @date 2024-03-13 16:50
*/
public class MyTestBean {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "MyTestBean{" +
"id='" + id + '\'' +
'}';
}
public MyTestBean(String id) {
this.id = id;
}
public MyTestBean() {
}
}
入口main方法
package com.spring.config.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 源码入口
*
* @author 1
* @version 1.0
* @description: TODO
* @date 2024-05-08 10:41
*/
public class Day1Demo {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:bean.xml");
Object myTestBean = applicationContext.getBean("myTestBean");
System.out.println(myTestBean);
}
}
2.进入源码,入门看一下
debug模式,F7
static {
//急切地加载ContextClosedEvent类以避免奇怪的类加载器问题WebLogic 8.1中的应用程序关闭。(达斯汀·伍兹报道)
//me:可能是作者也解决不了的问题,如果不加载这个类,程序无法启动
// Eagerly load the ContextClosedEvent class to avoid weird classloader issues
// on application shutdown in WebLogic 8.1. (Reported by Dustin Woods.)
ContextClosedEvent.class.getName();
}
/**
* Create a new ClassPathXmlApplicationContext with the given parent,
* loading the definitions from the given XML files.
* @param configLocations array of resource locations
* @param refresh whether to automatically refresh the context,
* loading all bean definitions and creating all singletons.
* Alternatively, call refresh manually after further configuring the context.
* @param parent the parent context
* @throws BeansException if context creation failed
* @see #refresh()
*/
public ClassPathXmlApplicationContext(
String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
throws BeansException {
//调用父类的构造方法,进行相关对象的创建
super(parent);
//设置配置,替换占位符
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}
可以看到核心方法refresh()的确只要搞懂了里面的13个方法,就算是对spring源码搞懂了
3.super(parent)方法详解





首先进入this()方法




进入setParent(parent)方法
Environment环境信息对象

开启xml文件验证(当然还有dtd文件验证等,都是提前规定好了,xml文件的格式,然后spring启动的时候,提前验证文件合法性)
该类是ApplicationContext实用的基础类实现,从包含bean定义的XML文档中绘制配置XmlBeanDefinitionReader(这个就是xml对应的bean定义信息读取器)
可以看一下这个方法XmlBeanDefinitionReader

然后看父类AbstractBeanDefinitionReader是抽象的bean定义信息读取器基础类

然后看对应子类,会发现该抽象类下面继承了不同的 文件对象读取器;
不同的BeanDefinitionReader 根据对应的文件规则,读取不同的文件,都转换成为统一的BeanDefinition,这就是spring源码的扩展性,如果以后有不同文件需要读取,只要源码扩展对应的实现类就可以了.

总结:
super(parent)方法都干了啥
1.创建了一系列的父类方法
2.创建了资源模式处理器,也就是用来解析系统运行需要的资源,例如,配置文件解析的一些规则等
3.设置上下文setParent()如果parent不为空,还会获取一些环境信息,并且进行一些环境信息的merge合并操作(后续用到在说)
700

被折叠的 条评论
为什么被折叠?



