
作者:小傅哥
部落格:https://bugstack.cn
沉澱、分享、成長,讓自己和他人都能有所收獲!😄
一、前言
你寫的代碼,能接的住産品加需求嗎?
接,是能接的,接幾次也行,哪怕就一個類一片的 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 容器中。整體設計結構如下圖:
- 資源加載器屬于相對獨立的部分,它位于 Spring 架構核心包下的IO實作内容,主要用于處理Class、本地和雲環境中的檔案資訊。
- 當資源可以加載後,接下來就是解析和注冊 Bean 到 Spring 中的操作,這部分實作需要和 DefaultListableBeanFactory 核心類結合起來,因為你所有的解析後的注冊動作,都會把 Bean 定義資訊放入到這個類中。
- 那麼在實作的時候就設計好接口的實作層級關系,包括我們需要定義出 Bean 定義的讀取接口
以及做好對應的實作類,在實作類中完成對 Bean 對象的解析和注冊。BeanDefinitionReader
四、實作
1. 工程結構
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
工程源碼:
公衆号「bugstack蟲洞棧」,回複:Spring 專欄,擷取完整源碼
Spring Bean 容器資源加載和使用類關系,如圖 6-3
- 本章節為了能把 Bean 的定義、注冊和初始化交給 Spring.xml 配置化處理,那麼就需要實作兩大塊内容,分别是:資源加載器、xml資源處理類,實作過程主要以對接口
Resource
的實作,而另外ResourceLoader
接口則是對資源的具體使用,将配置資訊注冊到 Spring 容器中去。BeanDefinitionReader
- 在 Resource 的資源加載器的實作中包括了,ClassPath、系統檔案、雲配置檔案,這三部分與 Spring 源碼中的設計和實作保持一緻,最終在 DefaultResourceLoader 中做具體的調用。
- 接口:BeanDefinitionReader、抽象類:AbstractBeanDefinitionReader、實作類:XmlBeanDefinitionReader,這三部分内容主要是合理清晰的處理了資源讀取後的注冊 Bean 容器操作。接口管定義,抽象類處理非接口功能外的注冊Bean元件填充,最終實作類即可隻關心具體的業務實作
另外本章節還參考 Spring 源碼,做了相應接口的內建和實作的關系,雖然這些接口目前還并沒有太大的作用,但随着架構的逐漸完善,它們也會發揮作用。如圖 6-4
- BeanFactory,已經存在的 Bean 工廠接口用于擷取 Bean 對象,這次新增加了按照類型擷取 Bean 的方法:
<T> T getBean(String name, Class<T> requiredType)
- ListableBeanFactory,是一個擴充 Bean 工廠接口的接口,新增加了
getBeansOfType
方法,在 Spring 源碼中還有其他擴充方法。getBeanDefinitionNames()
- HierarchicalBeanFactory,在 Spring 源碼中它提供了可以擷取父類 BeanFactory 方法,屬于是一種擴充工廠的層次子接口。Sub-interface implemented by bean factories that can be part of a hierarchy.
- AutowireCapableBeanFactory,是一個自動化處理Bean工廠配置的接口,目前案例工程中還沒有做相應的實作,後續逐漸完善。
- ConfigurableBeanFactory,可擷取 BeanPostProcessor、BeanClassLoader等的一個配置化接口。
- ConfigurableListableBeanFactory,提供分析和修改Bean以及預先執行個體化的操作接口,不過目前隻有一個 getBeanDefinition 方法。
2. 資源加載接口定義和實作
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 上。
3. 包裝資源加載器
按照資源加載的不同方式,資源加載器可以把這些方式集中到統一的類服務下進行處理,外部使用者隻需要傳遞資源位址即可,簡化使用。
定義接口: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 類實作的過程簡單,但這也是設計模式約定的具體結果,像是這裡不會讓外部調用放知道過多的細節,而是僅關心具體調用結果即可。
4. Bean定義讀取接口
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(),都是用于提供給後面三個方法的工具,加載和注冊,這兩個方法的實作會包裝到抽象類中,以免污染具體的接口實作方法。
5. Bean定義抽象類實作
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 中操作了
6. 解析XML處理Bean注冊
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 方法,處理資源加載,這裡新增加了一個内部方法:
,它主要負責解析 xmldoLoadBeanDefinitions
- 在 doLoadBeanDefinitions 方法中,主要是對xml的讀取
和元素 Element 解析。在解析的過程中通過循環操作,以此擷取 Bean 配置以及配置中的 id、name、class、value、ref 資訊。XmlUtil.readXML(inputStream)
- 最終把讀取出來的配置資訊,建立成 BeanDefinition 以及 PropertyValue,最終把完整的 Bean 定義内容注冊到 Bean 容器:
getRegistry().registerBeanDefinition(beanName, beanDefinition)
五、測試
1. 事先準備
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屬性的依賴了。
2. 配置檔案
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 注冊功能。
3. 單元測試(資源加載)
案例
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,可能加載時會慢
4. 單元測試(配置檔案注冊Bean)
@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 以及配置屬性資訊的内容,交給了
類讀取 Spring.xml 的方式來處理,并通過了測試驗證。new XmlBeanDefinitionReader(beanFactory)
六、總結
- 此時的工程結構已經越來越有 Spring 架構的味道了,以配置檔案為入口解析和注冊 Bean 資訊,最終再通過 Bean 工廠擷取 Bean 以及做相應的調用操作。
- 關于案例中每一個步驟的實作小傅哥這裡都會盡可能參照 Spring 源碼的接口定義、抽象類實作、名稱規範、代碼結構等,做相應的簡化處理。這樣大家在學習的過程中也可以通過類名或者接口和整個結構體學習 Spring 源碼,這樣學習起來就容易多了。
- 看完絕對不等于會,你隻有動起手來從一個小小的工程架構結構,敲到現在以及以後不斷的變大、變多、變強時,才能真的掌握這裡面的知識。另外每一個章節的功能實作都會涉及到很多的代碼設計思路,要認真去領悟。當然實踐起來是最好的領悟方式!
七、系列推薦
- 給你一台伺服器,你能把你寫的代碼部署到線上嗎?
- 畢業前寫了20萬行代碼,讓我從成為同學眼裡的面霸!
- 數學,離一個程式員有多近?
- 一次代碼評審,差點過不了試用期!
- 以數學知識點開始,深入講解 Java 的核心技術400頁Java面經
公衆号:bugstack蟲洞棧 | 作者小傅哥多年從事一線網際網路 Java 開發的學習曆程技術彙總,旨在為大家提供一個清晰詳細的學習教程,側重點更傾向編寫Java核心内容。如果能為您提供幫助,請給予支援(關注、點贊、分享)!