实验环境:spring-framework-5.0.2、jdk8、gradle4.3.1

上文介绍了一些常用容器及初始化、Bean解析过程,源码里面的实现比较复杂,如果我们想要自己实现一个IOC容器,自定义bean配置文件该怎么做呢?
其实只要了解核心思路实现起来是很简单的,只需3步:
1)继承AbstractApplicationContext 
2)读取配置文件,封装成beanDefinition对象,存入beanDefinitionMap中
3)调用refresh方法

1、定义配置文件 新建一个文件,格式如下:`user`是bean的名字,`beans.User`是bean的类路径,其中的`id`、`name`是字段属性值

user=beans.User{id:001,name:小明}
user2=beans.User2{id:002,name:小明2}
package beans;

import org.springframework.stereotype.Component;

@Component
public class User { private int id;
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}

2、既然我们定义好了配置文件格式,那么就需要一个BeanDefinitionReader来针对这类文件进行解析

package context;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils; /**
* 自定义BeanDefinitionReader
*/
public class MyBeanDefinitionReader extends AbstractBeanDefinitionReader { protected final Log logger = LogFactory.getLog(getClass()); public MyBeanDefinitionReader(BeanDefinitionRegistry registry) {
super(registry);
} @Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
// 解析配置文件
List<BeanDef> beanDefList = parseResource(resource); beanDefList.forEach(beanDef -> {
// 构造BeanDefinition
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
.genericBeanDefinition(beanDef.getClassPath());
// 填充参数
beanDef.getParamMap().forEach(beanDefinitionBuilder::addPropertyValue);
// 注册
super.getBeanFactory().registerBeanDefinition(beanDef.getId(),
beanDefinitionBuilder.getBeanDefinition());
});
return beanDefList.size();
} private List<BeanDef> parseResource(Resource resource) {
List<BeanDef> beanDefList = new ArrayList<>();
BufferedReader reader = null; try {
reader = new BufferedReader(new BufferedReader(new InputStreamReader(resource.getInputStream())));
String line = reader.readLine();
while (!StringUtils.isBlank(line)) {
BeanDef beanDef = buildBeanDef(line);
if (!beanDefList.contains(beanDef)) {
beanDefList.add(beanDef);
} else {
throw new RuntimeException("bean定义重复" + beanDef);
}
line = reader.readLine();
}
reader.close(); } catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return beanDefList;
} private BeanDef buildBeanDef(String line) {
BeanDef beanDef = new BeanDef();
String[] split = line.split("="); String id = split[0];
beanDef.setId(id); String source = split[1];
if (source.contains("{") && source.contains("}")) {
int begin = source.indexOf("{");
int end = source.indexOf("}"); String classPath = source.substring(0, begin);
beanDef.setClassPath(classPath); String params = source.substring(begin + 1, end);
String[] paramArray = params.split(","); for (String param : paramArray) {
String[] kv = param.split(":");
beanDef.getParamMap().put(kv[0], kv[1]);
}
} else {
beanDef.setClassPath(source);
}
return beanDef;
} public static class BeanDef {
private String id;
private String classPath;
private Map<String, Object> paramMap = new HashMap<>(); public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getClassPath() {
return classPath;
} public void setClassPath(String classPath) {
this.classPath = classPath;
} public Map<String, Object> getParamMap() {
return paramMap;
} @Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BeanDef beanDef = (BeanDef) o;
return id.equals(beanDef.id) &&
classPath.equals(beanDef.classPath);
} @Override
public int hashCode() {
return Objects.hash(id, classPath);
} @Override
public String toString() {
return "BeanDef{" +
"id='" + id + '\'' +
", classPath='" + classPath + '\'' +
", paramMap=" + paramMap +
'}';
}
}
}

3、现在我们可以读取bean配置文件,然后解析成BeanDefinition注册到容器中,于是我们再准备一个容器

package context;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionCustomizer;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert; /**
* 自定义容器实现
*/
public class MyApplicationContext extends AbstractApplicationContext { private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); // 创建自定义bean解析器,传入bean注册器beanFactory
private final MyBeanDefinitionReader definitionReader = new MyBeanDefinitionReader(beanFactory); public MyApplicationContext(String resourcePath) {
this.definitionReader.loadBeanDefinitions(new ClassPathResource(resourcePath));
this.refresh();
} /**
* 设置父容器,比如spring mvc容器作为子容器,父容器是spring容器
*/
@Override
public void setParent(@Nullable ApplicationContext parent) {
super.setParent(parent);
this.beanFactory.setParentBeanFactory(getInternalParentBeanFactory());
} @Override
public void setId(String id) {
super.setId(id);
} public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) {
this.beanFactory.setAllowBeanDefinitionOverriding(allowBeanDefinitionOverriding);
} public void setAllowCircularReferences(boolean allowCircularReferences) {
this.beanFactory.setAllowCircularReferences(allowCircularReferences);
} //---------------------------------------------------------------------
// ResourceLoader / ResourcePatternResolver override if necessary
//---------------------------------------------------------------------
@Override
public Resource getResource(String location) {
return super.getResource(location);
} @Override
public Resource[] getResources(String locationPattern) throws IOException {
return super.getResources(locationPattern);
} @Override
public void setClassLoader(@Nullable ClassLoader classLoader) {
super.setClassLoader(classLoader);
} @Override
@Nullable
public ClassLoader getClassLoader() {
return super.getClassLoader();
} //---------------------------------------------------------------------
// Implementations of AbstractApplicationContext's template methods
//--------------------------------------------------------------------- @Override
protected final void refreshBeanFactory() throws IllegalStateException {
this.beanFactory.setSerializationId(getId());
} @Override
protected void cancelRefresh(BeansException ex) {
this.beanFactory.setSerializationId(null);
super.cancelRefresh(ex);
} @Override
protected final void closeBeanFactory() {
this.beanFactory.setSerializationId(null);
} @Override
public final ConfigurableListableBeanFactory getBeanFactory() {
return this.beanFactory;
} public final DefaultListableBeanFactory getDefaultListableBeanFactory() {
return this.beanFactory;
} @Override
public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
assertBeanFactoryActive();
return this.beanFactory;
} // 子类重写
@Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) {
if (beanFactory instanceof DefaultListableBeanFactory) {
DefaultListableBeanFactory factory = (DefaultListableBeanFactory) beanFactory;
String[] beanDefinitionNames = factory.getBeanDefinitionNames();
System.out.println("step4.1 postProcessBeanFactory子类重写,已经加载beanDefinitionNames " + Arrays.asList(beanDefinitionNames));
} } // 子类重写
@Override
protected void onRefresh() throws BeansException {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
System.out.println("step9.1 onRefresh子类重写,已经加载beanDefinitionNames " + Arrays.asList(beanDefinitionNames)); }
}

4、测试一下bean能否正常注入到容器中

public class Main {

	public static void main(String[] args) {
testMyApplicationContext();
} private static void testMyApplicationContext() {
String path = "/config/mycontext.txt";
MyApplicationContext context = new MyApplicationContext(path);
System.out.println(context.getBean("user"));
context.close();
}
}

参考资料:

《Spring5核心原理与30个类手写》作者 谭勇德

《Spring源码深度解析》作者 郝佳

《Spring技术内幕》作者 计文柯

Spring源码-IOC部分-自定义IOC容器及Bean解析注册【4】的更多相关文章

  1. Spring源码分析(四)容器的基础XmlBeanFactory

    摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 经过Spring源码分析(二)容器基本用法和Spring源码分析(三)容 ...

  2. Spring源码剖析4:其余方式获取Bean的过程分析

    原型Bean加载过程 之前的文章,分析了非懒加载的单例Bean整个加载过程,除了非懒加载的单例Bean之外,Spring中还有一种Bean就是原型(Prototype)的Bean,看一下定义方式: 1 ...

  3. Spring学习之——手写Spring源码V2.0(实现IOC、D、MVC、AOP)

    前言 在上一篇<Spring学习之——手写Spring源码(V1.0)>中,我实现了一个Mini版本的Spring框架,在这几天,博主又看了不少关于Spring源码解析的视频,受益匪浅,也 ...

  4. spring源码学习之路---IOC初探(二)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章当中我没有提及具体的搭 ...

  5. Spring 源码学习(1)—— 容器的基本实现

    最近在读Spring的源码,参考的是郝佳的<Spring源码深度解析>,这里把一些学习心得分享一下,总结的地方可能还有一些不完善,希望大家指教 IoC(控制反转)是Spring的特性之一, ...

  6. Spring 源码学习 04:初始化容器与 DefaultListableBeanFactory

    前言 在前一篇文章:创建 IoC 容器的几种方式中,介绍了四种方式,这里以 AnnotationConfigApplicationContext 为例,跟进代码,看看 IoC 的启动流程. 入口 从 ...

  7. Spring源码分析(三)容器核心类

    摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 在上一篇文章中,我们熟悉了容器的基本用法.在这一篇,我们开始分析Spri ...

  8. Spring 源码学习(1) —— 自定义标签

    Spring 工作流程是先加载解析xml配置文件:配置文件中存在默认的标签,也可以自定义标签.解析默认标签调用: private void parseDefaultElement(Element el ...

  9. Spring源码情操陶冶-自定义节点的解析

    本文承接前文Spring源码情操陶冶-DefaultBeanDefinitionDocumentReader#parseBeanDefinitions,特开辟出一块新地来啃啃这块有意思的骨头 自定义节 ...

随机推荐

  1. D. Substring

    D. Substring 题意: 给你一个有向图,然后给你一串字符串第i个点的值为第i个字符,然后给你m条有向边,从中找一条路径然后这条路径中点的值相同的个数的最大值,如果图有环输出-1. 思路: 拓 ...

  2. 如何让 Spring Security 「少管闲事」

    记两种让 Spring Security「少管闲事」的方法. 遇到问题 一个应用对外提供 Rest 接口,接口的访问认证通过 Spring Security OAuth2 控制,token 形式为 J ...

  3. [Guide]Google Python Style Guide

    扉页 项目主页 Google Style Guide Google 开源项目风格指南 - 中文版 背景 Python 是Google主要的脚本语言.这本风格指南主要包含的是针对python的编程准则. ...

  4. 基于Spring MVC + Spring + MyBatis的【物流系统 - 公司信息管理】

    资源下载:https://download.csdn.net/download/weixin_44893902/45601768 练习点设计:模糊查询.删除.新增 一.语言和环境 实现语言:JAVA语 ...

  5. Java程序设计基础笔记 • 【第6章 循环结构进阶】

    全部章节   >>>> 本章目录 6.1 for循环 6.1.1 for循环的简介 6.1.2 for循环的使用 6.1.3 for循环的表达式 6.1.4 实践练习 6.2 ...

  6. JDK、JVM和JRE三者间的关系,及JDK安装路径下的文件夹说明

    JDK的全称是Java SE Development Kit, 即Java标准开发包,是Sun公司提供的一套用于开发Java应用程序的开发包, 它提供了编译.运行Java查询所需的各种工具和资源,包括 ...

  7. Windows Server 2016 服务器安装IIS

    1. 打开服务器管理器,点击[添加角色和功能选项]  2.进入[添加角色和功能向导]页面,点击"下一步"  3.安装类型选择[基于角色或基于功能的安装],点击"下一步&q ...

  8. Redis缓存安装Version5.0.7

    1.说明 Redis是一个开源(BSD许可)的, 内存中的数据结构存储系统, 它可以用作数据库.缓存和消息中间件. 这里介绍在Linux下使用源码编译安装的方式. 2.下载 官方下载地址:https: ...

  9. Ranger-Hdfs插件安装

    Ranger-Hdfs插件ranger-0.6.0-hdfs-plugin安装到Hdfs的所有NameNode节点, 其他的DataNode节点不需要安装. 1. 登陆hdfs安装的用户,hdfs/z ...

  10. PHP 中的僵尸进程、孤儿进程详解

    僵尸进程 当子进程运行结束,父进程仍然继续运行,但父进程没有对子进程进行回收,释放子进程占用的资源,此时子进程就成为了一个僵尸进程. 在Unix进程管理中,如果新开的子进程运行结束,父进程将会收到一个 ...