微信公众号:bugstack虫洞栈 | 博客:https://bugstack.cn
沉淀、分享、成长,专注于原创专题案例,以最易学习编程的方式分享知识,让自己和他人都能有所收获。目前已完成的专题有;Netty4.x实战专题案例、用Java实现JVM、基于JavaAgent的全链路监控、手写RPC框架、架构设计专题案例[Ing]等。
你用剑🗡、我用刀🔪,好的代码都很烧😏,望你不吝出招💨!
往往简单的背后都有人为你承担着不简单,Spring 就是这样的家伙!而分析它的源码就像鬼吹灯,需要寻龙、点穴、分金、定位,最后往往受点伤(时间)、流点血(精力)、才能获得宝藏(成果)。
另外鉴于之前分析spring-mybatis、quartz,一篇写了将近2万字,内容过于又长又干,挖藏师好辛苦,看戏的也憋着肾,所以这次分析spring源码分块解读,便于理解、便于消化。
那么,这一篇就从 bean 的加载开始,从 xml 配置文件解析 bean 的定义,到注册到 Spring 的核心类 DefaultListableBeanFactory ,盗墓过程如下;
从上图可以看到从 xml 解析出 bean 到注册完成需要经历过8个核心类以及22个方法跳转流程,这也是本文后面需要重点分析的内容。好!那么就当为了你的钱程一起盗墓吧!
对于源码分析一定要单独列一个简单的工程,一针见血的搞你最需要的地方,模拟、验证、调试。现在这个案例工程还很简单,随着后面分析内容的增加,会不断的扩充。整体工程可以下载,可以关注公众号:bugstack虫洞栈 | 回复:源码分析
itstack-demo-code-spring
└── src
├── main
│ ├── java
│ │ └── org.itstack.demo
│ │ └── UserService.java
│ └── resources
│ └── spring-config.xml
└── test
└── java
└── org.itstack.demo.test
└── ApiTest.java
整个 bean 注册过程核心功能包括;配置文件加载、工厂创建、XML解析、Bean定义、Bean注册,执行流程如下;
从上图的注册 bean 流程看到,核心类包括;
好!摸金分金定穴完事,搬山的搬山、卸岭的卸岭,开始搞!
UserService.java & 定义一个 bean,Spring 万物皆可 bean
public class UserService {
public String queryUserInfo(Long userId) {
return "花花 id:" + userId;
}
}
spring-config.xml & 在 xml 配置 bean 内容
<?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-3.0.xsd"
default-autowire="byName">
<bean id="userService" class="org.itstack.demo.UserService" scope="singleton"/>
</beans>
ApiTest.java & 单元测试类
@Test
public void test_XmlBeanFactory() {
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
UserService userService = beanFactory.getBean("userService", UserService.class);
logger.info("测试结果:{}", userService.queryUserInfo(1000L));
}
@Test
public void test_ClassPathXmlApplicationContext() {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-config.xml");
UserService userService = beanFactory.getBean("userService", UserService.class);
logger.info("测试结果:{}", userService.queryUserInfo(1000L));
}
两个单测方法都可以做结果输出,但是 XmlBeanFactory 已经标记为 @Deprecated 就是告诉我们这个墓穴啥也没有了,被盗过了,别看了。好!我们也不看他了,我们看现在推荐的2号墓 ClassPathXmlApplicationContext
如上不出意外正确结果如下;
23:34:24.699 [main] INFO org.itstack.demo.test.ApiTest - 测试结果:花花 id:1000
Process finished with exit code 0
在整个 bean 的注册过程中,xml 解析是非常大的一块,也是非常重要的一块。如果顺着 bean 工厂初始化分析,那么一层层扒开,就像陈玉楼墓穴挖到一半,遇到大蜈蚣一样难缠。所以我们先把蜈蚣搞定!
@Test
public void test_DocumentLoader() throws Exception {
// 设置资源
EncodedResource encodedResource = new EncodedResource(new ClassPathResource("spring-config.xml"));
// 加载解析
InputSource inputSource = new InputSource(encodedResource.getResource().getInputStream());
DocumentLoader documentLoader = new DefaultDocumentLoader();
Document doc = documentLoader.loadDocument(inputSource, new ResourceEntityResolver(new PathMatchingResourcePatternResolver()), new DefaultHandler(), 3, false);
// 输出结果
Element root = doc.getDocumentElement();
NodeList nodeList = root.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (!(node instanceof Element)) continue;
Element ele = (Element) node;
if (!"bean".equals(ele.getNodeName())) continue;
String id = ele.getAttribute("id");
String clazz = ele.getAttribute("class");
String scope = ele.getAttribute("scope");
logger.info("测试结果 beanName:{} beanClass:{} scope:{}", id, clazz, scope);
}
}
可能初次看这段代码还是有点晕的,但这样提炼出来单独解决,至少给你一种有抓手的感觉。在 spring 解析 xml 时候首先是将配置资源交给 ClassPathResource ,再通过构造函数传递给 EncodedResource;
private EncodedResource(Resource resource, String encoding, Charset charset) {
super();
Assert.notNull(resource, "Resource must not be null");
this.resource = resource;
this.encoding = encoding;
this.charset = charset;
}
以上这个过程还是比较简单的,只是一个初始化过程。接下来是通过 Document 解析处理 xml 文件。这个过程是仿照 spring 创建时候需要的参数信息进行组装,如下;
documentLoader.loadDocument(inputSource, new ResourceEntityResolver(new PathMatchingResourcePatternResolver()), new DefaultHandler(), 3, false);
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
if (logger.isDebugEnabled()) {
logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");
}
DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
return builder.parse(inputSource);
}
通过上面的代码获取 org.w3c.dom.Document, Document 里面此时包含了所有 xml 的各个 Node 节点信息,最后输出节点内容,如下;
Element root = doc.getDocumentElement();
NodeList nodeList = root.getChildNodes();
好!测试一下,正常情况下,结果如下;
23:47:00.249 [main] INFO org.itstack.demo.test.ApiTest - 测试结果 beanName:userService beanClass:org.itstack.demo.UserService scope:singleton
Process finished with exit code 0
可以看到的我们的 xml 配置内容已经完完整整的取出来了,接下来就交给 spring 进行处理了。红姑娘、鹧鸪哨、咱们出发!
ClassPathXmlApplicationContext.java & 截取部分代码
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
throws BeansException {
super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}
AbstractApplicationContext.java & 部分代码截取
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// 设置容器初始化
prepareRefresh();
// 让子类进行 BeanFactory 初始化,并且将 Bean 信息 转换为 BeanFinition,最后注册到容器中
// 注意,此时 Bean 还没有初始化,只是配置信息都提取出来了
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
...
}
}
AbstractApplicationContext.java & 部分代码截取
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
}
return beanFactory;
}
AbstractRefreshableApplicationContext.java & 部分代码截取
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
AbstractXmlApplicationContext.java & 部分代码截取
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(this.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);
}
AbstractXmlApplicationContext.java & 部分代码截取
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
Resource[] configResources = getConfigResources();
if (configResources != null) {
reader.loadBeanDefinitions(configResources);
}
String[] configLocations = getConfigLocations();
if (configLocations != null) {
reader.loadBeanDefinitions(configLocations);
}
}
AbstractBeanDefinitionReader.java & 部分代码截取
public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
Assert.notNull(locations, "Location array must not be null");
int counter = 0;
for (String location : locations) {
counter += loadBeanDefinitions(location);
}
return counter;
}
AbstractBeanDefinitionReader.java & 部分代码截取
public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
return loadBeanDefinitions(location, null);
}
AbstractBeanDefinitionReader.java & 部分代码截取
public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
ResourceLoader resourceLoader = getResourceLoader();
if (resourceLoader == null) {
throw new BeanDefinitionStoreException(
"Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
}
if (resourceLoader instanceof ResourcePatternResolver) {
// Resource pattern matching available.
try {
Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
int loadCount = loadBeanDefinitions(resources);
if (actualResources != null) {
for (Resource resource : resources) {
actualResources.add(resource);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
}
return loadCount;
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"Could not resolve bean definition resource pattern [" + location + "]", ex);
}
}
else {
// Can only load single resources by absolute URL.
Resource resource = resourceLoader.getResource(location);
int loadCount = loadBeanDefinitions(resource);
if (actualResources != null) {
actualResources.add(resource);
}
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
}
return loadCount;
}
}
XmlBeanDefinitionReader.java & 部分代码截取
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
// 判断验证
...
try {
InputStream inputStream = encodedResource.getResource().getInputStream();
try {
InputSource inputSource = new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
}
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
finally {
inputStream.close();
}
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"IOException parsing XML document from " + encodedResource.getResource(), ex);
}
finally {
currentResources.remove(encodedResource);
if (currentResources.isEmpty()) {
this.resourcesCurrentlyBeingLoaded.remove();
}
}
}
XmlBeanDefinitionReader.java & 部分代码截取
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {
try {
Document doc = doLoadDocument(inputSource, resource);
return registerBeanDefinitions(doc, resource);
} catch(){}
}
DefaultBeanDefinitionDocumentReader.java & 部分代码截取
public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
this.readerContext = readerContext;
logger.debug("Loading bean definitions");
Element root = doc.getDocumentElement();
doRegisterBeanDefinitions(root);
}
DefaultBeanDefinitionDocumentReader.java & 部分代码截取
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate deleg
if (delegate.isDefaultNamespace(root)) {
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
if (delegate.isDefaultNamespace(ele)) {
parseDefaultElement(ele, delegate);
}
else {
delegate.parseCustomElement(ele);
}
}
}
}
else {
delegate.parseCustomElement(root);
}
}
DefaultBeanDefinitionDocumentReader.java & 部分代码截取
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
importBeanDefinitionResource(ele);
}
else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
processAliasRegistration(ele);
}
else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
processBeanDefinition(ele, delegate);
}
else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
// recurse
doRegisterBeanDefinitions(ele);
}
}
DefaultBeanDefinitionDocumentReader.java & 部分代码截取
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
if (bdHolder != null) {
bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
try {
// Register the final decorated instance.
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
}
catch (BeanDefinitionStoreException ex) {
getReaderContext().error("Failed to register bean definition with name '" +
bdHolder.getBeanName() + "'", ele, ex);
}
// Send registration event.
getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
}
}
BeanDefinitionReaderUtils.java & 部分代码截取
public static void registerBeanDefinition(
BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
throws BeanDefinitionStoreException {
// Register bean definition under primary name.
String beanName = definitionHolder.getBeanName();
registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
// Register aliases for bean name, if any.
String[] aliases = definitionHolder.getAliases();
if (aliases != null) {
for (String alias : aliases) {
registry.registerAlias(beanName, alias);
}
}
}
DefaultListableBeanFactory.java & 部分代码截取
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
Assert.hasText(beanName, "Bean name must not be empty");
Assert.notNull(beanDefinition, "BeanDefinition must not be null");
if (beanDefinition instanceof AbstractBeanDefinition) {
try {
((AbstractBeanDefinition) beanDefinition).validate();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
"Validation of bean definition failed", ex);
}
}
BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);
if (existingDefinition != null) {
...
}
else {
...
else {
// Still in startup registration phase
this.beanDefinitionMap.put(beanName, beanDefinition);
this.beanDefinitionNames.add(beanName);
this.manualSingletonNames.remove(beanName);
}
this.frozenBeanDefinitionNames = null;
}
if (existingDefinition != null || containsSingleton(beanName)) {
resetBeanDefinition(beanName);
}
}
源码853行: 这就是最终我们将 xml 中的配置信息注册到了配置中心,beanDefinitionMap,同时还会写入到 beanDefinitionNames
看下最终的注入结果,嗯!我们的盗墓挖到了一点宝物;
联系客服