你写的代码,能接的住产品加需求吗?
接,是能接的,接几次也行,哪怕就一个类一片的 if...else 也可以!但接完成什么样可就不一定了,会不会出事故也不是能控制住的。
那出事故时,你说因为我写 if...else 多了导致代码烂了,但可是你先动的手啊:你说的需求还得加、你说的老板让上线、你说的合同都签了,搬砖码农的我没办法,才以堆代码平需求,需求太多不好搞,我才以搬砖平需求!诸侯不服,我才以兵服诸侯,你不服,我就打到你服!
但代码烂了有时候并不是因为需求加的快、也不是着急上线。因为往往在承接产品需求的前几次,一个功能逻辑的设计并不会太复杂,也不会有多急迫,甚至会留出让你做设计、做评审、做开发的时间,如果这个时候仍不能把以后可能会发生的事情评估到需求里,那么导致代码的混乱从一开始就已经埋下了,以后只能越来越乱!
承接需求并能把它做好,这来自于对需求的理解,产品场景开发的经验以及对代码实践落地的把控能力等综合多方面因素的结果。就像你现在做的开发中,你的代码有哪些是经常变化的,有哪些是固定通用的,有哪些是负责逻辑拼装的、有哪些是来做核心实现的。那么现在如果你的核心共用层做了频繁变化的业务层包装,那么肯定的说,你的代码即将越来越乱,甚至可能埋下事故的风险!
在我们实现的 Spring 框架中,每一个章节都会结合上一章节继续扩展功能,就像每一次产品都在加需求一样,那么在学习的过程中可以承上启下的对照和参考,看看每一个模块的添加都是用什么逻辑和技术细节实现的。这些内容的学习,会非常有利于你以后在设计和实现,自己承接产品需求时做的具体开发,代码的质量也会越来越高,越来越有扩展性和可维护性。
在完成 Spring 的框架雏形后,现在我们可以通过单元测试进行手动操作 Bean 对象的定义、注册和属性填充,以及最终获取对象调用方法。但这里会有一个问题,就是如果实际使用这个 Spring 框架,是不太可能让用户通过手动方式创建的,而是最好能通过配置文件的方式简化创建过程。需要完成如下操作:
如图中我们需要把步骤:2、3、4整合到Spring框架中,通过 Spring 配置文件的方式将 Bean 对象实例化。接下来我们就需要在现有的 Spring 框架中,添加能解决 Spring 配置的读取、解析、注册Bean的操作。
依照本章节的需求背景,我们需要在现有的 Spring 框架雏形中添加一个资源解析器,也就是能读取classpath、本地文件和云文件的配置内容。这些配置内容就是像使用 Spring 时配置的 Spring.xml 一样,里面会包括 Bean 对象的描述和属性信息。 在读取配置文件信息后,接下来就是对配置文件中的 Bean 描述信息解析后进行注册操作,把 Bean 对象注册到 Spring 容器中。整体设计结构如下图:
BeanDefinitionReader
以及做好对应的实现类,在实现类中完成对 Bean 对象的解析和注册。small-spring-step-05
└── src
├── main
│ └── java
│ └── cn.bugstack.springframework
│ ├── beans
│ │ ├── factory
│ │ │ ├── factory
│ │ │ │ ├── AutowireCapableBeanFactory.java
│ │ │ │ ├── BeanDefinition.java
│ │ │ │ ├── BeanReference.java
│ │ │ │ ├── ConfigurableBeanFactory.java
│ │ │ │ └── SingletonBeanRegistry.java
│ │ │ ├── support
│ │ │ │ ├── AbstractAutowireCapableBeanFactory.java
│ │ │ │ ├── AbstractBeanDefinitionReader.java
│ │ │ │ ├── AbstractBeanFactory.java
│ │ │ │ ├── BeanDefinitionReader.java
│ │ │ │ ├── BeanDefinitionRegistry.java
│ │ │ │ ├── CglibSubclassingInstantiationStrategy.java
│ │ │ │ ├── DefaultListableBeanFactory.java
│ │ │ │ ├── DefaultSingletonBeanRegistry.java
│ │ │ │ ├── InstantiationStrategy.java
│ │ │ │ └── SimpleInstantiationStrategy.java
│ │ │ ├── support
│ │ │ │ └── XmlBeanDefinitionReader.java
│ │ │ ├── BeanFactory.java
│ │ │ ├── ConfigurableListableBeanFactory.java
│ │ │ ├── HierarchicalBeanFactory.java
│ │ │ └── ListableBeanFactory.java
│ │ ├── BeansException.java
│ │ ├── PropertyValue.java
│ │ └── PropertyValues.java
│ ├── core.io
│ │ ├── ClassPathResource.java
│ │ ├── DefaultResourceLoader.java
│ │ ├── FileSystemResource.java
│ │ ├── Resource.java
│ │ ├── ResourceLoader.java
│ │ └── UrlResource.java
│ └── utils
│ └── ClassUtils.java
└── test
└── java
└── cn.bugstack.springframework.test
├── bean
│ ├── UserDao.java
│ └── UserService.java
└── ApiTest.java
Spring Bean 容器资源加载和使用类关系,如图 6-3
Resource
、ResourceLoader
的实现,而另外 BeanDefinitionReader
接口则是对资源的具体使用,将配置信息注册到 Spring 容器中去。另外本章节还参考 Spring 源码,做了相应接口的集成和实现的关系,虽然这些接口目前还并没有太大的作用,但随着框架的逐步完善,它们也会发挥作用。如图 6-4
<T> T getBean(String name, Class<T> requiredType)
getBeansOfType
、getBeanDefinitionNames()
方法,在 Spring 源码中还有其他扩展方法。cn.bugstack.springframework.core.io.Resource
public interface Resource { InputStream getInputStream() throws IOException; }
在 Spring 框架下创建 core.io 核心包,在这个包中主要用于处理资源加载流。定义 Resource 接口,提供获取 InputStream 流的方法,接下来再分别实现三种不同的流文件操作:classPath、FileSystem、URL
ClassPath:cn.bugstack.springframework.core.io.ClassPathResource
public class ClassPathResource implements Resource { private final String path; private ClassLoader classLoader; public ClassPathResource(String path) { this(path, (ClassLoader) 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)
FileSystem:cn.bugstack.springframework.core.io.FileSystemResource
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文件输出到控制台。
Url:cn.bugstack.springframework.core.io.UrlResource
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){ if (con instanceof HttpURLConnection){ ((HttpURLConnection) con).disconnect(); } throw ex; } } }
通过 HTTP 的方式读取云服务的文件,我们也可以把配置文件放到 GitHub 或者 Gitee 上。
按照资源加载的不同方式,资源加载器可以把这些方式集中到统一的类服务下进行处理,外部用户只需要传递资源地址即可,简化使用。
定义接口:cn.bugstack.springframework.core.io.ResourceLoader
public interface ResourceLoader { /** * Pseudo URL prefix for loading from the class path: "classpath:" */ String CLASSPATH_URL_PREFIX = "classpath:"; Resource getResource(String location); }
定义获取资源接口,里面传递 location 地址即可。
实现接口:cn.bugstack.springframework.core.io.DefaultResourceLoader
public class DefaultResourceLoader implements ResourceLoader { @Override public Resource getResource(String location) { Assert.notNull(location, "Location must not be null"); 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 类实现的过程简单,但这也是设计模式约定的具体结果,像是这里不会让外部调用放知道过多的细节,而是仅关心具体调用结果即可。
cn.bugstack.springframework.beans.factory.support.BeanDefinitionReader
public interface BeanDefinitionReader { BeanDefinitionRegistry getRegistry(); ResourceLoader getResourceLoader(); 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(),都是用于提供给后面三个方法的工具,加载和注册,这两个方法的实现会包装到抽象类中,以免污染具体的接口实现方法。
cn.bugstack.springframework.beans.factory.support.AbstractBeanDefinitionReader
public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader { private final BeanDefinitionRegistry registry; private 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 中操作了
cn.bugstack.springframework.beans.factory.xml.XmlBeanDefinitionReader
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 { 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); } 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); } 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)
cn.bugstack.springframework.test.bean.UserDao
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); } }
cn.bugstack.springframework.test.bean.UserService
public class UserService { private String uId; private UserDao userDao; public void queryUserInfo() { return userDao.queryUserName(uId); } // ...get/set }
Dao、Service,是我们平常开发经常使用的场景。在 UserService 中注入 UserDao,这样就能体现出Bean属性的依赖了。
important.properties
# Config File
system.key=OLpj9823dZ
spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="userDao" class="cn.bugstack.springframework.test.bean.UserDao"/> <bean id="userService" class="cn.bugstack.springframework.test.bean.UserService"> <property name="uId" value="10001"/> <property name="userDao" ref="userDao"/> </bean> </beans>
这里有两份配置文件,一份用于测试资源加载器,另外 spring.xml 用于测试整体的 Bean 注册功能。
案例
private DefaultResourceLoader resourceLoader; @Before public void init() { resourceLoader = new DefaultResourceLoader(); } @Test public void test_classpath() throws IOException { Resource resource = resourceLoader.getResource("classpath:important.properties"); InputStream inputStream = resource.getInputStream(); String content = IoUtil.readUtf8(inputStream); System.out.println(content); } @Test public void test_file() throws IOException { Resource resource = resourceLoader.getResource("src/test/resources/important.properties"); InputStream inputStream = resource.getInputStream(); String content = IoUtil.readUtf8(inputStream); System.out.println(content); } @Test public void test_url() throws IOException { Resource resource = resourceLoader.getResource("https://github.com/fuzhengwei/small-spring/important.properties" InputStream inputStream = resource.getInputStream(); String content = IoUtil.readUtf8(inputStream); System.out.println(content); }
测试结果
# Config File
system.key=OLpj9823dZ
Process finished with exit code 0
这三个方法:test_classpath、test_file、test_url,分别用于测试加载 ClassPath、FileSystem、Url 文件,URL文件在Github,可能加载时会慢
案例
@Test public void test_xml() { // 1.初始化 BeanFactory DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); // 2. 读取配置文件&注册Bean XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions("classpath:spring.xml"); // 3. 获取Bean对象调用方法 UserService userService = beanFactory.getBean("userService", UserService.class); String result = userService.queryUserInfo(); System.out.println("测试结果:" + result); }
测试结果
测试结果:小傅哥
Process finished with exit code 0
在上面的测试案例中可以看到,我们把以前通过手动注册 Bean 以及配置属性信息的内容,交给了 new XmlBeanDefinitionReader(beanFactory)
类读取 Spring.xml 的方式来处理,并通过了测试验证。