目标
在完成 Spring 的框架雏形后,现在我们可以通过单元测试进行手动操作 Bean 对象的定义、注册和属性填充,以及最终获取对象调用方法。但这里会有一个问题,就是如果实际使用这个 Spring 框架,是不太可能让用户通过手动方式创建的,而是最好能通过配置文件的方式简化创建过程。需要完成如下操作:
如图中我们需要把步骤:2、3、4整合到Spring框架中,通过 Spring 配置文件的方式将 Bean 对象实例化。接下来我们就需要在现有的 Spring 框架中,添加能解决 Spring 配置的读取、解析、注册Bean的操作。
设计
依照本章节的需求背景,我们需要在现有的 Spring 框架雏形中添加一个资源解析器,也就是能读取classpath、本地文件和云文件的配置内容。这些配置内容就是像使用 Spring 时配置的 Spring.xml 一样,里面会包括 Bean 对象的描述和属性信息。在读取配置文件信息后,接下来就是对配置文件中的 Bean 描述信息解析后进行注册操作,把 Bean 对象注册到 Spring 容器中。整体设计结构如下图:
- 资源加载器属于相对独立的部分,它位于 Spring 框架核心包下的IO实现内容,主要用于处理Class、本地和云环境中的文件信息。
- 当资源可以加载后,接下来就是解析和注册 Bean 到 Spring 中的操作,这部分实现需要和
DefaultListableBeanFactory 核心类结合起来,因为你所有的解析后的注册动作,都会把 Bean
定义信息放入到这个类中。 - 那么在实现的时候就设计好接口的实现层级关系,包括我们需要定义出 Bean 定义的读取接口 BeanDefinitionReader以及做好对应的实现类,在实现类中完成对 Bean 对象的解析和注册。
实现
本章涉及到的类的结构图
- 本章节为了能把 Bean 的定义、注册和初始化交给 Spring.xml配置化处理,那么就需要实现两大块内容,分别是:资源加载器、xml资源处理类,实现过程主要以对接口Resource、ResourceLoader 的实现,而另外 BeanDefinitionReader接口则是对资源的具体使用,将配置信息注册到 Spring 容器中去。
- 在 Resource 的资源加载器的实现中包括了,ClassPath、系统文件、云配置文件,这三部分与 Spring源码中的设计和实现保持一致,最终在 DefaultResourceLoader 中做具体的调用。
- 接口:BeanDefinitionReader、抽象类:AbstractBeanDefinitionReader、实现类:XmlBeanDefinitionReader,这三部分内容主要是合理清晰的处理了资源读取后的注册Bean 容器操作。接口管定义,抽象类处理非接口功能外的注册Bean组件填充,最终实现类即可只关心具体的业务实现。
另外本章节还参考 Spring 源码,做了相应接口的集成和实现的关系,虽然这些接口目前还并没有太大的作用,但随着框架的逐步完善,它们也会发挥作用:
- BeanFactory,已经存在的 Bean 工厂接口用于获取 Bean 对象,这次新增加了按照类型获取 Bean 的方法: T getBean(String name, Class requiredType)
- ListableBeanFactory,是一个扩展 Bean 工厂接口的接口,新增加了 getBeansOfType、getBeanDefinitionNames() 方法,在 Spring 源码中还有其他扩展方法。
- HierarchicalBeanFactory,在 Spring 源码中它提供了可以获取父类 BeanFactory方法,属于是一种扩展工厂的层次子接口。
- AutowireCapableBeanFactory,是一个自动化处理Bean工厂配置的接口,目前案例工程中还没有做相应的实现,后续逐步完善。
- ConfigurableBeanFactory,可获取BeanPostProcessor、BeanClassLoader等的一个配置化接口。
- ConfigurableListableBeanFactory,提供分析和修改Bean以及预先实例化的操作接口,不过目前只有一个getBeanDefinition 方法。
资源加载接口定义和实现
package com.qingyun.springframework.core.io;
import java.io.IOException;
import java.io.InputStream;
/**
* @description: 有着Bean定义的资源以及相关操作
* @author: 張青云
* @create: 2021-08-20 10:06
**/
public interface Resource {
/**
* 从资源中获取输入流
* @return 输入流
* @throws IOException IO异常
*/
InputStream getInputStream() throws IOException;
}
在 Spring 框架下创建 core.io 核心包,在这个包中主要用于处理资源加载流。定义 Resource 接口,提供获取 InputStream 流的方法,接下来再分别实现三种不同的流文件操作:classPath、FileSystem、URL。
package com.qingyun.springframework.core.io;
import cn.hutool.core.lang.Assert;
import com.qingyun.springframework.util.ClassUtils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* @description: 资源存放在类路径下
* @author: 張青云
* @create: 2021-08-20 10:08
**/
public class ClassPathResource implements Resource {
private final String path;
private final ClassLoader classLoader;
public ClassPathResource(String path) {
this(path, null);
}
public ClassPathResource(String path, ClassLoader classLoader) {
Assert.notNull(path, "Path must not be null");
this.path = path;
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
}
@Override
public InputStream getInputStream() throws IOException {
// 使用类加载器获取输入流
InputStream is = classLoader.getResourceAsStream(path);
if (is == null) {
throw new FileNotFoundException(
this.path + " cannot be opened because it does not exist");
}
return is;
}
}
这一部分的实现是用于通过 ClassLoader 读取ClassPath 下的文件信息,具体的读取过程主要是:classLoader.getResourceAsStream(path)。
package com.qingyun.springframework.core.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @description: 资源存放在文件当中
* @author: 張青云
* @create: 2021-08-20 10:14
**/
public class FileSystemResource implements Resource {
private final File file;
private final String path;
public FileSystemResource(File file) {
this.file = file;
this.path = file.getPath();
}
public FileSystemResource(String path) {
this.file = new File(path);
this.path = path;
}
@Override
public InputStream getInputStream() throws IOException {
return new FileInputStream(this.file);
}
public final String getPath() {
return this.path;
}
}
通过指定文件路径的方式读取文件信息,这部分大家肯定还是非常熟悉的,经常会读取一些txt、excel文件输出到控制台。
package com.qingyun.springframework.core.io;
import cn.hutool.core.lang.Assert;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
/**
* @description: 资源存放在网络上
* @author: 張青云
* @create: 2021-08-20 10:15
**/
public class UrlResource implements Resource{
private final URL url;
public UrlResource(URL url) {
Assert.notNull(url,"URL must not be null");
this.url = url;
}
@Override
public InputStream getInputStream() throws IOException {
URLConnection con = this.url.openConnection();
try {
return con.getInputStream();
} catch (IOException ex){
throw ex;
} finally {
if (con instanceof HttpURLConnection){
((HttpURLConnection) con).disconnect();
}
}
}
}
通过 HTTP 的方式读取云服务的文件,我们也可以把配置文件放到 GitHub 或者 Gitee 上。
包装资源加载器
按照资源加载的不同方式,资源加载器可以把这些方式集中到统一的类服务下进行处理,外部用户只需要传递资源地址即可,简化使用。
package com.qingyun.springframework.core.io;
/**
* @description: 使用外观模式来屏蔽掉Resource接口实现系统的细节,使得客户端可以不用去了解有哪些Resource的实现类
* @author: 張青云
* @create: 2021-08-20 10:18
**/
public interface ResourceLoader {
// 类路径的前缀
String CLASSPATH_URL_PREFIX = "classpath:";
/**
* 根据用户输入的资源地址location获取一个对应的Resource
* @param location 资源地址
* @return Resource
*/
Resource getResource(String location);
}
定义获取资源接口,里面传递 location 地址即可。
package com.qingyun.springframework.core.io;
import cn.hutool.core.lang.Assert;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @description:
* @author: 張青云
* @create: 2021-08-20 10:26
**/
public class DefaultResourceLoader implements ResourceLoader {
@Override
public Resource getResource(String location) {
Assert.notNull(location, "Location must not be null");
// location以类路径前缀开头时返回类路径资源解析器
if (location.startsWith(CLASSPATH_URL_PREFIX)) {
return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
} else {
try {
URL url = new URL(location);
return new UrlResource(url);
} catch (MalformedURLException e) {
return new FileSystemResource(location);
}
}
}
}
在获取资源的实现中,主要是把三种不同类型的资源处理方式进行了包装,分为:判断是否为ClassPath、URL以及文件。虽然 DefaultResourceLoader 类实现的过程简单,但这也是设计模式约定的具体结果,像是这里不会让外部调用放知道过多的细节,而是仅关心具体调用结果即可。
Bean定义读取接口
package com.qingyun.springframework.beans.factory.support;
import com.qingyun.springframework.beans.BeansException;
import com.qingyun.springframework.core.io.Resource;
import com.qingyun.springframework.core.io.ResourceLoader;
/**
* @description: 从bean定义的资源resource中解析bean的定义并且将其注册
* @author: 張青云
* @create: 2021-08-20 10:29
**/
public interface BeanDefinitionReader {
/**
* 获取实例注册表
*/
BeanDefinitionRegistry getRegistry();
/**
* 获取资源加载器
*/
ResourceLoader getResourceLoader();
/**
* 从resource中解析并创建bean的定义信息,并且将其注册到注册表中
*/
void loadBeanDefinitions(Resource resource) throws BeansException;
void loadBeanDefinitions(Resource... resources) throws BeansException;
void loadBeanDefinitions(String location) throws BeansException;
}
这是一个 Simple interface for bean definition readers. 其实里面无非定义了几个方法,包括:getRegistry()、getResourceLoader(),以及三个加载Bean定义的方法。这里需要注意 getRegistry()、getResourceLoader(),都是用于提供给后面三个方法的工具,加载和注册,这两个方法的实现会包装到抽象类中,以免污染具体的接口实现方法。
Bean定义抽象类实现
package com.qingyun.springframework.beans.factory.support;
import com.qingyun.springframework.core.io.DefaultResourceLoader;
import com.qingyun.springframework.core.io.ResourceLoader;
/**
* @author: 張青云
* @create: 2021-08-20 10:38
**/
public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader {
private final BeanDefinitionRegistry registry;
private final ResourceLoader resourceLoader;
protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {
this(registry, new DefaultResourceLoader());
}
public AbstractBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {
this.registry = registry;
this.resourceLoader = resourceLoader;
}
@Override
public BeanDefinitionRegistry getRegistry() {
return registry;
}
@Override
public ResourceLoader getResourceLoader() {
return resourceLoader;
}
}
抽象类把 BeanDefinitionReader 接口的前两个方法全部实现完了,并提供了构造函数,让外部的调用使用方,把Bean定义注入类,传递进来。这样在接口 BeanDefinitionReader 的具体实现类中,就可以把解析后的 XML 文件中的 Bean 信息,注册到 Spring 容器去了。以前我们是通过单元测试使用,调用 BeanDefinitionRegistry 完成Bean的注册,现在可以放到 XMl 中操作了
解析XML处理Bean注册
package com.qingyun.springframework.beans.factory.xml;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import com.qingyun.springframework.beans.BeansException;
import com.qingyun.springframework.beans.factory.PropertyValue;
import com.qingyun.springframework.beans.factory.config.BeanDefinition;
import com.qingyun.springframework.beans.factory.config.BeanReference;
import com.qingyun.springframework.beans.factory.support.AbstractBeanDefinitionReader;
import com.qingyun.springframework.beans.factory.support.BeanDefinitionRegistry;
import com.qingyun.springframework.core.io.Resource;
import com.qingyun.springframework.core.io.ResourceLoader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.io.IOException;
import java.io.InputStream;
/**
* @description: 当定义bean的资源resource是Xml类型的文件时,解析Xml文件并生成Bean的定义信息,然后将其注册
* @author: 張青云
* @create: 2021-08-20 10:46
**/
public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
super(registry);
}
public XmlBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {
super(registry, resourceLoader);
}
@Override
public void loadBeanDefinitions(Resource resource) throws BeansException {
try (InputStream inputStream = resource.getInputStream()) {
doLoadBeanDefinitions(inputStream);
} catch (IOException | ClassNotFoundException e) {
throw new BeansException("IOException parsing XML document from " + resource, e);
}
}
@Override
public void loadBeanDefinitions(Resource... resources) throws BeansException {
for (Resource resource : resources) {
loadBeanDefinitions(resource);
}
}
@Override
public void loadBeanDefinitions(String location) throws BeansException {
ResourceLoader resourceLoader = getResourceLoader();
Resource resource = resourceLoader.getResource(location);
loadBeanDefinitions(resource);
}
/**
* 解析XML文件并完成Bean定义信息的注册
*/
protected void doLoadBeanDefinitions(InputStream inputStream) throws ClassNotFoundException {
Document doc = XmlUtil.readXML(inputStream);
Element root = doc.getDocumentElement();
NodeList childNodes = root.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
// 判断元素
if (!(childNodes.item(i) instanceof Element)) continue;
// 判断对象
if (!"bean".equals(childNodes.item(i).getNodeName())) continue;
// 解析标签
Element bean = (Element) childNodes.item(i);
String id = bean.getAttribute("id");
String name = bean.getAttribute("name");
String className = bean.getAttribute("class");
// 获取 Class,方便获取类中的名称
Class<?> clazz = Class.forName(className);
// 优先级 id > name
String beanName = StrUtil.isNotEmpty(id) ? id : name;
if (StrUtil.isEmpty(beanName)) {
beanName = StrUtil.lowerFirst(clazz.getSimpleName());
}
// 定义Bean
BeanDefinition beanDefinition = new BeanDefinition(clazz);
// 读取属性并填充
for (int j = 0; j < bean.getChildNodes().getLength(); j++) {
if (!(bean.getChildNodes().item(j) instanceof Element)) continue;
if (!"property".equals(bean.getChildNodes().item(j).getNodeName())) continue;
// 解析标签:property
Element property = (Element) bean.getChildNodes().item(j);
String attrName = property.getAttribute("name");
String attrValue = property.getAttribute("value");
String attrRef = property.getAttribute("ref");
// 获取属性值:引入对象、值对象
Object value = StrUtil.isNotEmpty(attrRef) ? new BeanReference(attrRef) : attrValue;
// 创建属性信息
PropertyValue propertyValue = new PropertyValue(attrName, value);
beanDefinition.getPropertyValues().addPropertyValue(propertyValue);
}
// 判断是否已有该Bean的定义
if (getRegistry().containsBeanDefinition(beanName)) {
throw new BeansException("Duplicate beanName[" + beanName + "] is not allowed");
}
// 注册 BeanDefinition
getRegistry().registerBeanDefinition(beanName, beanDefinition);
}
}
}
XmlBeanDefinitionReader 类最核心的内容就是对 XML 文件的解析,把我们本来在代码中的操作放到了通过解析 XML 自动注册的方式。
loadBeanDefinitions 方法,处理资源加载,这里新增加了一个内部方法:doLoadBeanDefinitions,它主要负责解析 xml在 doLoadBeanDefinitions 方法中,主要是对xml的读取XmlUtil.readXML(inputStream) 和元素 Element 解析。在解析的过程中通过循环操作,以此获取 Bean 配置以及配置中的 id、name、class、value、ref 信息。最终把读取出来的配置信息,创建成 BeanDefinition 以及 PropertyValue,最终把完整的 Bean 定义内容注册到 Bean 容器:getRegistry().registerBeanDefinition(beanName, beanDefinition)
测试
事先准备
package com.qingyun.springframework.beansTest;
import java.util.HashMap;
import java.util.Map;
/**
* @description:
* @author: 張青云
* @create: 2021-08-19 00:13
**/
public class UserDao {
private static Map<String, String> hashMap = new HashMap<>();
static {
hashMap.put("10001", "青云先生");
hashMap.put("10002", "八杯水");
hashMap.put("10003", "阿毛");
}
public String queryUserName(String uId) {
return hashMap.get(uId);
}
}
package com.qingyun.springframework.beansTest;
/**
* @description:
* @author: 張青云
* @create: 2021-08-18 22:54
**/
public class UserService {
private String uId;
private UserDao userDao;
public UserService() {
}
public void queryUserInfo() {
System.out.println("查询用户信息:" + userDao.queryUserName(uId));
}
}
测试用例
@Test
public void test_xml() {
// 1.初始化 BeanFactory
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
// 2. 读取配置文件&注册Bean
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions(new DefaultResourceLoader().getResource("classpath:spring.xml"));
// 3. 获取Bean对象调用方法
UserService userService = beanFactory.getBean("userService", UserService.class);
userService.queryUserInfo();
}
XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="userDao" class="com.qingyun.springframework.beansTest.UserDao"/>
<bean id="userService" class="com.qingyun.springframework.beansTest.UserService">
<property name="uId" value="10001"/>
<property name="userDao" ref="userDao"/>
</bean>
</beans>
结果
项目代码Github地址:https://github.com/Zhang-Qing-Yun/mini-spring,本节代码对应的commit标识为ff6db8d欢迎标星