当前位置: 首页 > news >正文

Spring 中的 BeanDefinitionParserDelegate 和 NamespaceHandler

一、BeanDefinitionParserDelegate

Spring在解析xml文件的时候,在遇到<bean>标签的时候,我们会使用BeanDefinitionParserDelegate对象类解析<bean>标签的内容,包括<bean>标签的多个属性,例如 id name class init-method destory-method等,还包括各种子标签 例如 <properties> <constructor>

org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#doRegisterBeanDefinitions

/*** Register each bean definition within the given root {@code <beans/>} element.*/@SuppressWarnings("deprecation")  // for Environment.acceptsProfiles(String...)protected void doRegisterBeanDefinitions(Element root) {// Any nested <beans> elements will cause recursion in this method. In// order to propagate and preserve <beans> default-* attributes correctly,// keep track of the current (parent) delegate, which may be null. Create// the new (child) delegate with a reference to the parent for fallback purposes,// then ultimately reset this.delegate back to its original (parent) reference.// this behavior emulates a stack of delegates without actually necessitating one.BeanDefinitionParserDelegate parent = this.delegate;this.delegate = createDelegate(getReaderContext(), root, parent);if (this.delegate.isDefaultNamespace(root)) {String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);if (StringUtils.hasText(profileSpec)) {String[] specifiedProfiles = StringUtils.tokenizeToStringArray(profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);// We cannot use Profiles.of(...) since profile expressions are not supported// in XML config. See SPR-12458 for details.if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {if (logger.isDebugEnabled()) {logger.debug("Skipped XML bean definition file due to specified profiles [" + profileSpec +"] not matching: " + getReaderContext().getResource());}return;}}}preProcessXml(root);parseBeanDefinitions(root, this.delegate);postProcessXml(root);this.delegate = parent;}

 可以看到这里 this.delegate 是一个 BeanDefinitionParserDelegate 对象

 接下来的几层调用中,传递的都是 this.delegate 这个BeanDefinitionParserDelegate 对象

org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#parseBeanDefinitions
 

/*** Parse the elements at the root level in the document:* "import", "alias", "bean".* @param root the DOM root element of the document*/protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {if (delegate.isDefaultNamespace(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;if (delegate.isDefaultNamespace(ele)) {parseDefaultElement(ele, delegate);}else {delegate.parseCustomElement(ele);}}}}else {delegate.parseCustomElement(root);}}

 

二、NamespaceHandler

http://www.lryc.cn/news/484914.html

相关文章:

  • BERT模型核心组件详解及其实现
  • 图论-代码随想录刷题记录[JAVA]
  • c#加载shellcode
  • HarmonyOS 开发环境搭建
  • 【网络云计算】2024第46周周考-磁盘管理的基础知识-RAID篇
  • 深入理解 SQL_MODE 之 ANSI_QUOTES
  • 容器技术在持续集成与持续交付中的应用
  • 【嵌入式软件-STM32】OLED显示屏+调试方法
  • kubernetes简单入门实战
  • Python连接Mysql、Postgre、ClickHouse、Redis常用库及封装方法
  • 如何修改npm包
  • Django 2024全栈开发指南(三):数据库模型与ORM操作(上篇)
  • 低代码可视化-uniapp开关选择组件-低码生成器
  • 【arxiv‘24】Vision-Language Navigation with Continual Learning
  • 如何在 Ubuntu 上安装 Jupyter Notebook
  • 免费申请 Let‘s Encrypt SSL 证书
  • 【JAVA】Java基础—面向对象编程:继承—重写父类方法
  • 【C++初阶】C++入门
  • 自然推理系统:的拒取式的解析
  • OceanBase 分区表详解
  • Java中 LinkedList<>,ArrayDeque<>的区别 || Queue和Deque的区别
  • freemarker 读取template.xml ,通过response 输出文件,解决中文乱码问题
  • arkUI:水果选择与管理:基于 ArkUI 的长按编辑功能实现
  • docker使用,docker图形化界面+docker详细命令
  • idea项目运行时 java: 错误: 不支持发行版本 21
  • hive alter table add columns 是否使用 cascade 的方案
  • 手机怎么玩steam游戏?随时随地远程串流玩steam游戏教程
  • 【使用antv g6实现拓扑图】
  • 【数学 函数空间】拉普拉斯变换解微分方程步骤
  • vue3: toRef, reactive, toRefs, toRaw