chapter05_從spring.xml讀取Bean

一、簡化Bean的注冊

如果每次注冊一個Bean,都要像上節一樣,手動寫PropertyValues相關的代碼,那太復雜了,我們希望讀取XML文件,自動注冊Bean,這樣對于使用者,甚至不知道有BeanDefinition的存在

二、統一處理資源文件

新建資源接口,Spring對所有的資源文件,統一處理

  • 一個資源,最重要的就是拿到輸入流,拿到輸入流就可以讀取文件
public interface Resource {InputStream getInputStream() throws IOException;
}

提供三個資源實現類,分別讀取不同類型的文件,這就是策略模式

類路徑下的文件(最常用)

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 : ClassUtil.getClassLoader();}@Overridepublic InputStream getInputStream() throws IOException {InputStream is = classLoader.getResourceAsStream(path);if (is == null) {throw new FileNotFoundException(path + " cannot be opened because it does not exist");}return is;}
}

文件系統下的文件

public class FileSystemResource implements Resource{private File file;public FileSystemResource(File file) {this.file = file;}@Overridepublic InputStream getInputStream() throws IOException {return Files.newInputStream(file.toPath());}
}

網絡文件

public class UrlResource implements Resource{private final URL url;public UrlResource(URL url) {Assert.notNull(url,"URL must not be null");this.url = url;}@Overridepublic InputStream getInputStream() throws IOException {URLConnection con = url.openConnection();return con.getInputStream();}
}

資源加載器接口,簡化資源類的使用,自動根據路徑選擇合適的加載類

  • 這又屬于工廠方法設計模式
/*** @Author 孤風雪影* @Email gitee.com/efairy520* @Date 2025/1/2 22:16* @Version 1.0*/
public interface ResourceLoader {String CLASSPATH_URL_PREFIX = "classpath:";Resource getResource(String location);
}

資源加載器接口實現

  • 根據路徑前綴,默認就是使用classpath策略
/*** @Author 孤風雪影* @Email gitee.com/efairy520* @Date 2025/1/2 22:18* @Version 1.0*/
public class DefaultResourceLoader implements ResourceLoader{@Overridepublic 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(new File(location));}}}
}

三、從文件中讀取Bean

定義BeanDefinitionReader接口,從文件中讀取BeanDefinition,并且注冊到Bean工廠,這里有三要素

  • 資源文件
  • Bean工廠
  • 讀取BeanDefinition的邏輯(單個資源,多個資源,位置字符串)
/*** @Author 孤風雪影* @Email gitee.com/efairy520* @Date 2025/1/2 22:26* @Version 1.0*/
public interface BeanDefinitionReader {BeanDefinitionRegistry getRegistry();ResourceLoader getResourceLoader();void loadBeanDefinitions(Resource resource);void loadBeanDefinitions(Resource... resources);void loadBeanDefinitions(String location);
}

用抽象類AbstractBeanDefinitionReader實現接口,模板方法設計模式

  • Bean工廠和資源加載器都是確定的,抽象類直接實現
  • 只有加載BeanDefinition是不確定的邏輯,交給具體的策略子類實現
public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader {private final BeanDefinitionRegistry registry;private final ResourceLoader resourceLoader;public AbstractBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {this.registry = registry;this.resourceLoader = resourceLoader;}public AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {this(registry, new DefaultResourceLoader());}@Overridepublic BeanDefinitionRegistry getRegistry() {return registry;}@Overridepublic ResourceLoader getResourceLoader() {return resourceLoader;}}

XmlBeanDefinitionReader做具體實現,策略模式

public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {public XmlBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {super(registry, resourceLoader);}public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {super(registry);}@Overridepublic void loadBeanDefinitions(Resource resource) {try {InputStream is = resource.getInputStream();doLoadBeanDefinitions(is);} catch (IOException e) {throw new RuntimeException("IOException parsing XML document from " + resource, e);}}@Overridepublic void loadBeanDefinitions(Resource... resources) {for (Resource resource : resources) {loadBeanDefinitions(resource);}}@Overridepublic void loadBeanDefinitions(String location) {ResourceLoader resourceLoader = getResourceLoader();Resource resource = resourceLoader.getResource(location);loadBeanDefinitions(resource);}/*** 真正解析XMl文件的方法** @param inputStream*/private void doLoadBeanDefinitions(InputStream inputStream) {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 = null;try {clazz = Class.forName(className);} catch (ClassNotFoundException e) {throw new RuntimeException("不存在的類名" + className);}// 優先級 id > name,此處是Bean自己的id和nameString beanName = StrUtil.isNotEmpty(id) ? id : name;if (StrUtil.isEmpty(beanName)) {beanName = StrUtil.lowerFirst(clazz.getSimpleName());}// 定義BeanBeanDefinition 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;// 解析標簽:propertyElement 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 RuntimeException("Duplicate beanName[" + beanName + "] is not allowed");}// 注冊 BeanDefinitiongetRegistry().registerBeanDefinition(beanName, beanDefinition);}}
}

BeanDefinitionReader接口、資源接口,層次結構圖

請添加圖片描述

四、測試

新建Person類

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class Person {private String name;private int age;private Cat cat;
}

新建Cat類

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class Cat {private String name;private int weight;
}

編寫一個spring.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans><bean id="cat" class="cn.shopifymall.springframework.test.bean.Cat"><property name="name" value="tomcat"/><property name="weight" value="2000"/></bean><bean id="person" class="cn.shopifymall.springframework.test.bean.Person"><property name="name" value="10001"/><property name="age" value="18"/><property name="cat" ref="cat"/></bean></beans>

新建測試類

public class ApiTest {@Testpublic void testGetBeanFromXml() {// 1.初始化 BeanFactoryDefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();// 2. 讀取配置文件&注冊BeanXmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);reader.loadBeanDefinitions("classpath:spring.xml");// 3. 獲取Bean對象調用方法Person person = (Person) beanFactory.getBean("person");System.out.println("person:" + person);}
}

控制臺輸出

person:Person(name=10001, age=18, cat=Cat(name=tomcat, weight=2000))

五、總結

  • 通過引入spring.xml配置文件,我們就可以簡化Bean的注冊
  • 用戶只需要編寫一個xml文件,由XmlBeanDefinitionReader自動解析xml文件,生成BeanDefinition并注冊到BeanFactory

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/96581.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/96581.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/96581.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【數位DP】D. From 1 to Infinity

Problem - D - Codeforces 題目&#xff1a; 思路&#xff1a; 數位DP 數論 題目讓我們求這個無限序列 123456789101112.... 的前 k 個數的數位和 題目看起來很不好求&#xff0c;事實上確實是這樣的 我們可以先從簡單問題開始 問題①. 求 k 位置對應著第幾個數 那么顯然…

gitlab、jenkins等應用集成ldap

gitlab、jenkins等應用集成ldap 文檔 openldap安裝 -添加條目gitlab、jenkins等應用集成ldap gitlab集成ldap gitlab版本&#xff1a;gitlab-jh-17.7.0 ldap版本&#xff1a;openldap-2.6.10 修改/etc/gitlab/gitlab.rb文件&#xff0c;編輯相關信息 gitlab_rails[ldap_en…

Unity中國小游戲行業沙龍:抖音小游戲平臺分析與規劃

目錄 一、抖音小游戲市場全景分析 行業現狀與發展趨勢 行業發展關鍵議題 內容運營生態觀察 二、平臺技術架構與運營體系 用戶復訪與留存體系 技術支撐體系 三、平臺激勵與商業化政策 收益分成機制 資金服務升級 技術基礎建設 四、生態合作與發展規劃 開發者支持體系…

手機橫屏適配方案

CSS自動旋轉頁面實戰指南在移動端開發中&#xff0c;橫屏適配是一個常見但棘手的問題。本文將深入解析一套完整的CSS橫屏適配方案&#xff0c;讓你的網頁在手機旋轉時自動調整布局&#xff0c;提供無縫的用戶體驗。一、橫屏適配的重要性 隨著移動設備使用場景的多樣化&#xff…

藍橋杯算法之基礎知識(2)——Python賽道

1.循環里面套用遞歸&#xff0c;當遞歸執行return時&#xff0c;只會退出當前遞歸層2.不能一邊遍歷list 一邊pop解決辦法&#xff1a;倒序遍歷解決或者創建新的列表去存儲3.sqrt求出來的始終是小數形式&#xff0c;注意題目要求的結果有可能是整型你直接sqrt就提交&#xff0c;…

如何優雅解決 OpenCV 分段錯誤(Segfault):子進程隔離實戰

在分布式數據平臺&#xff08;如 Databricks Spark&#xff09;中跑視頻處理任務時&#xff0c;你是否遇到過這種惡心的報錯&#xff1f;Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe. : org.apache.spark.Spark…

Docker的六種網絡模式(詳解)

文章目錄1. bridge&#xff08;默認&#xff09;2. host3. none4. container5. overlay6. macvlan7. 總結對比Docker 六種網絡模式是容器網絡的基礎概念&#xff0c;不同模式決定容器與宿主機、外部網絡、其他容器之間的通信方式。 1. bridge&#xff08;默認&#xff09; Br…

微服務流量分發核心:Spring Cloud 負載均衡解析

目錄 理解負載均衡 負載均衡的實現方式 服務端負載均衡 客戶端負載均衡 Spring Cloud LoadBalancer快速上手 常見的負載均衡策略 自定義負載均衡策略 LoadBalancer 原理 理解負載均衡 在 Spring Cloud 微服務架構中&#xff0c;負載均衡&#xff08;Load Balance&#…

鴻蒙異步處理從入門到實戰:Promise、async/await、并發池、超時重試全套攻略

摘要&#xff08;介紹目前的背景和現狀&#xff09; 在鴻蒙&#xff08;HarmonyOS&#xff09;里&#xff0c;網絡請求、文件操作、數據庫訪問這類 I/O 都是異步的。主流寫法跟前端類似&#xff1a;Promise、async/await、回調。想把 app 做得“流暢且不阻塞”&#xff0c;核心…

【html2img/pdf 純!純!python將html保存為圖片/pdf!!效果非常的棒!】

素材 a.png html card.html <!DOCTYPE html> <html lang"zh-CN"><head><meta charset"UTF-8"><title>固定樣式卡片</title><style>/* 基礎樣式和頁面居中 */body {font-family: "微軟雅黑", "P…

帶寬評估(三)lossbase_v2

一、優化方向 調整丟包恢復算法的參數:可以通過調整算法中的一些參數,如丟包恢復速率、丟包恢復閾值等,來優化算法的性能。 調整發送窗口大小:在固定丟包場景下,可以通過調整發送窗口大小來控制發送速率,從而減少丟包率。 a=fmtp:96 x-google-min-bitrate=300 二、Goo…

imx6ull-驅動開發篇29——Linux阻塞IO 實驗

目錄 實驗程序編寫 blockio.c blockioApp.c Makefile 文件 運行測試 在之前的文章里&#xff0c;Linux阻塞和非阻塞 IO&#xff08;上&#xff09;&#xff0c;我們學習了Linux應用程序了兩種操作方式&#xff1a;阻塞和非阻塞 IO。 在Linux 中斷實驗中&#xff0c;Linux…

97. 小明逛公園,Floyd 算法,127. 騎士的攻擊,A * 算法

97. 小明逛公園Floyd 算法dijkstra, bellman_ford 是求單個起點到單個終點的最短路徑&#xff0c;dijkstra無法解決負權邊的問題&#xff0c; bellman_ford解決了負權邊的問題&#xff0c;但二者都是基于單起點和單終點。而Floyd 算法旨在解決多個起點到多個終點的最短路徑問題…

?崩壞世界觀中的安全漏洞與哲學映射:從滲透測試視角解構虛擬秩序的脆弱性?

?崩壞世界觀&#xff1a;游戲中的世界&#xff0c;是真實&#xff0c;也是虛幻的&#xff01;對于游戲中的NPC角色而言&#xff0c;TA們生存的世界&#xff0c;是真實的&#xff01;對于游戲玩家而言&#xff0c;游戲中的世界&#xff0c;是虛擬的&#xff01;通過沉浸式的游戲…

【離線安裝】CentOS Linux 7 上離線部署Oracle 19c(已成功安裝2次)

1.部署參考鏈接&#xff1a; CentOS 7 rpm方式離線安裝 Oracle 19chttps://blog.csdn.net/Vampire_1122/article/details/123038137?fromshareblogdetail&sharetypeblogdetail&sharerId123038137&sharereferPC&sharesourceweixin_45806267&sharefromfrom…

小白向:Obsidian(Markdown語法學習)快速入門完全指南:從零開始構建你的第二大腦(免費好用的筆記軟件的知識管理系統)、黑曜石筆記

一、認識Obsidian&#xff1a;不只是筆記軟件的知識管理系統 1.1 什么是Obsidian Obsidian是一個基于本地存儲的知識管理系統&#xff0c;它將你的所有筆記以純文本Markdown格式保存在電腦本地。這個名字來源于黑曜石——一種火山熔巖快速冷卻形成的玻璃質巖石&#xff0c;象…

攻防世界—Confusion1—(模板注入ssti)

一.解題在login和register的頁面中發現這個文件路徑接下去就找有什么點可以利用二.ssti通過題目信息可知是一只蛇把一只大象纏繞起來了&#xff0c;蛇代表python&#xff0c;大象代表php這邊通過python可以推測可能是模板注入&#xff0c;這邊我看其他的解題是說通過看報文信息…

【Protues仿真】基于AT89C52單片機的超聲波測距

目錄 1 HCSR04超聲波測距傳感器 1.1 基本參數 1.2 引腳說明 1.3 工作原理&#xff08;時序圖&#xff09; 2 基于AT89C52單片機的超聲波測距電路原理圖 2.1 硬件連接說明 2.2 工作原理 3 基于AT89C52單片機的超聲波測距控制程序 3.1.1 初始化設置 3.1.2 超聲波測距原…

LLM - Agent核心架構:四大“身體”部件

文章目錄一、Agent核心架構&#xff1a;四大“身體”部件1. 核心大腦&#xff1a;大型語言模型&#xff08;LLM&#xff09;2. 記憶系統&#xff1a;短期與長期記憶3. 工具箱&#xff08;Toolkit&#xff09;&#xff1a;從“思想家”到“行動家”4. 驅動循環&#xff08;Engin…

html-docx-js 導出word

2025.08.23今天我學習了如何將html頁面內容導出到word中&#xff0c;并保持原有格式&#xff0c;效果如下&#xff1a;代碼如下&#xff1a;1&#xff1a;列表頁面按鈕<el-button type"warning" plain icon"el-icon-download" size"mini" cli…