在終端輸入命令:
```shell
git checkout -b 04
```
創建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/io/Resource.java` ,其內容為:
```java
package net.zhaoxuyang.ioc.bean.io;
import java.io.IOException;
import java.io.InputStream;
public interface Resource {
InputStream getInputStream() throws IOException;
}
```
創建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/io/UrlResource.java` ,其內容為:
```java
package net.zhaoxuyang.ioc.bean.io;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class UrlResource implements Resource {
private final URL url;
public UrlResource(URL url) {
this.url = url;
}
@Override
public InputStream getInputStream() throws IOException{
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
return urlConnection.getInputStream();
}
}
```
創建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/io/ResourceLoader.java` ,其內容為:
```java
package net.zhaoxuyang.ioc.bean.io;
import java.net.URL;
public class ResourceLoader {
public Resource getResource(String location) {
URL resource = this.getClass().getClassLoader().getResource(location);
return new UrlResource(resource);
}
}
```
創建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/BeanDefinitionReader.java` ,其內容為:
```java
package net.zhaoxuyang.ioc.bean;
public interface BeanDefinitionReader {
void loadBeanDefinitions(String location) throws Exception;
}
```
創建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/AbstractBeanDefinitionReader.java` ,其內容為:
```java
package net.zhaoxuyang.ioc.bean;
import java.util.HashMap;
import java.util.Map;
import net.zhaoxuyang.ioc.bean.io.ResourceLoader;
public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader{
private Map<String,BeanDefinition> registry;
private ResourceLoader resourceLoader;
protected AbstractBeanDefinitionReader(ResourceLoader resourceLoader){
this.registry = new HashMap<>(16);
this.resourceLoader = resourceLoader;
}
public Map<String,BeanDefinition> getRegistry(){
return registry;
}
public ResourceLoader getResourceLoader(){
return resourceLoader;
}
}
```
將`net.zhaoxuyang.ioc.bean` 下的以下三個類,移動到 `net.zhaoxuyang.ioc.bean.factory` 包下:
- AbstractBeanFactory
- AutowireCapableBeanFactory
- BeanFactory
創建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/bean/xml/XmlBeanDefinitionReader.java` ,其內容為:
```java
package net.zhaoxuyang.ioc.bean.xml;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import net.zhaoxuyang.ioc.bean.AbstractBeanDefinitionReader;
import net.zhaoxuyang.ioc.bean.BeanDefinition;
import net.zhaoxuyang.ioc.bean.PropertyValue;
import net.zhaoxuyang.ioc.bean.io.ResourceLoader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
public XmlBeanDefinitionReader(ResourceLoader resourceLoader) {
super(resourceLoader);
}
@Override
public void loadBeanDefinitions(String location) throws Exception {
InputStream inputStream = getResourceLoader().getResource(location).getInputStream();
doLoadBeanDefinitions(inputStream);
}
protected void doLoadBeanDefinitions(InputStream inputStream) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc = docBuilder.parse(inputStream);
// 解析bean
registerBeanDefinitions(doc);
inputStream.close();
}
public void registerBeanDefinitions(Document doc) {
Element root = doc.getDocumentElement();
parseBeanDefinitions(root);
}
protected void parseBeanDefinitions(Element 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;
processBeanDefinition(ele);
}
}
}
protected void processBeanDefinition(Element ele) {
String name = ele.getAttribute("name");
String className = ele.getAttribute("class");
BeanDefinition beanDefinition = new BeanDefinition();
processProperty(ele,beanDefinition);
beanDefinition.setBeanClassName(className);
getRegistry().put(name, beanDefinition);
}
private void processProperty(Element ele,BeanDefinition beanDefinition) {
NodeList propertyNode = ele.getElementsByTagName("property");
for (int i = 0; i < propertyNode.getLength(); i++) {
Node node = propertyNode.item(i);
if (node instanceof Element) {
Element propertyEle = (Element) node;
String name = propertyEle.getAttribute("name");
String value = propertyEle.getAttribute("value");
beanDefinition.getPropertyValues().addPropertyValue(new PropertyValue(name,value));
}
}
}
}
```
創建文件 `/Code/z8g/ioc/src/test/resources/ioc.xml` ,其內容為:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<bean name="testService" class="net.zhaoxuyang.ioc.TestService">
<property name="text" value="Hello World!"></property>
</bean>
</beans>
```
創建文件 `/Code/z8g/ioc/src/test/java/net/zhaoxuyang/ioc/bean/xml/XmlBeanDefinitionReaderTest.java` ,其內容為:
```java
package net.zhaoxuyang.ioc.bean.xml;
import java.util.Map;
import net.zhaoxuyang.ioc.bean.BeanDefinition;
import net.zhaoxuyang.ioc.bean.io.ResourceLoader;
import org.junit.Assert;
import org.junit.Test;
public class XmlBeanDefinitionReaderTest {
@Test
public void testLoadBeanDefinitions() throws Exception {
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(new ResourceLoader());
reader.loadBeanDefinitions("ioc.xml");
Map<String, BeanDefinition> registry = reader.getRegistry();
Assert.assertTrue(registry.size() > 0);
}
}
```
在終端輸入以下命令:
```shell
$ git add .
$ git commit -m 'config-beanfactory-with-xml'
```
- 空白目錄
- 精簡版Spring的實現
- 0 前言
- 1 注冊和獲取bean
- 2 抽象工廠實例化bean
- 3 注入bean屬性
- 4 通過XML配置beanFactory
- 5 將bean注入到bean
- 6 加入應用程序上下文
- 7 JDK動態代理實現的方法攔截器
- 8 加入切入點和aspectj
- 9 自動創建AOP代理
- Redis原理
- 1 Redis簡介與構建
- 1.1 什么是Redis
- 1.2 構建Redis
- 1.3 源碼結構
- 2 Redis數據結構與對象
- 2.1 簡單動態字符串
- 2.1.1 sds的結構
- 2.1.2 sds與C字符串的區別
- 2.1.3 sds主要操作的API
- 2.2 雙向鏈表
- 2.2.1 adlist的結構
- 2.2.2 adlist和listNode的API
- 2.3 字典
- 2.3.1 字典的結構
- 2.3.2 哈希算法
- 2.3.3 解決鍵沖突
- 2.3.4 rehash
- 2.3.5 字典的API
- 2.4 跳躍表
- 2.4.1 跳躍表的結構
- 2.4.2 跳躍表的API
- 2.5 整數集合
- 2.5.1 整數集合的結構
- 2.5.2 整數集合的API
- 2.6 壓縮列表
- 2.6.1 壓縮列表的結構
- 2.6.2 壓縮列表結點的結構
- 2.6.3 連鎖更新
- 2.6.4 壓縮列表API
- 2.7 對象
- 2.7.1 類型
- 2.7.2 編碼和底層實現
- 2.7.3 字符串對象
- 2.7.4 列表對象
- 2.7.5 哈希對象
- 2.7.6 集合對象
- 2.7.7 有序集合對象
- 2.7.8 類型檢查與命令多態
- 2.7.9 內存回收
- 2.7.10 對象共享
- 2.7.11 對象空轉時長
- 3 單機數據庫的實現
- 3.1 數據庫
- 3.1.1 服務端中的數據庫
- 3.1.2 切換數據庫
- 3.1.3 數據庫鍵空間
- 3.1.4 過期鍵的處理
- 3.1.5 數據庫通知
- 3.2 RDB持久化
- 操作系統
- 2021-01-08 Linux I/O 操作
- 2021-03-01 Linux 進程控制
- 2021-03-01 Linux 進程通信
- 2021-06-11 Linux 性能優化
- 2021-06-18 性能指標
- 2022-05-05 Android 系統源碼閱讀筆記
- Java基礎
- 2020-07-18 Java 前端編譯與優化
- 2020-07-28 Java 虛擬機類加載機制
- 2020-09-11 Java 語法規則
- 2020-09-28 Java 虛擬機字節碼執行引擎
- 2020-11-09 class 文件結構
- 2020-12-08 Java 內存模型
- 2021-09-06 Java 并發包
- 代碼性能
- 2020-12-03 Java 字符串代碼性能
- 2021-01-02 ASM 運行時增強技術
- 理解Unsafe
- Java 8
- 1 行為參數化
- 1.1 行為參數化的實現原理
- 1.2 Java 8中的行為參數化
- 1.3 行為參數化 - 排序
- 1.4 行為參數化 - 線程
- 1.5 泛型實現的行為參數化
- 1.6 小結
- 2 Lambda表達式
- 2.1 Lambda表達式的組成
- 2.2 函數式接口
- 2.2.1 Predicate
- 2.2.2 Consumer
- 2.2.3 Function
- 2.2.4 函數式接口列表
- 2.3 方法引用
- 2.3.1 方法引用的類別
- 2.3.2 構造函數引用
- 2.4 復合方法
- 2.4.1 Comparator復合
- 2.4.2 Predicate復合
- 2.4.3 Function復合
- 3 流處理
- 3.1 流簡介
- 3.1.1 流的定義
- 3.1.2 流的特點
- 3.2 流操作
- 3.2.1 中間操作
- 3.2.2 終端操作
- 3.3.3 構建流
- 3.3 流API
- 3.3.1 flatMap的用法
- 3.3.2 reduce的用法
- 3.4 collect操作
- 3.4.1 collect示例
- 3.4.2 Collector接口