Spring PropertyPlaceholderConfigurer多配置问题

devtools/2025/2/6 0:49:04/

本文重点是通过例子代码的debug了解PropertyPlaceholderConfigurer的原理

更多可阅读:placeholderconfigurer文档 了解

目录

    • 测试程序如下
    • PropertyPlaceholderConfigurer
      • placeholderConfigurer1 & placeholderConfigurer2的执行
      • userbean的BeanDefinition应用PropertyPlaceholderConfigurer前:
      • userbean的BeanDefinition应用PropertyPlaceholderConfigurer后:
    • userBean的name的set
    • 总结

测试程序如下

java">public class TestPropertyPlaceholderConfigurer {public static void main(String[] args) throws Exception{ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("application.xml");UserBean userBean = (UserBean) applicationContext.getBean("userBean");System.out.println(userBean.getName());}
}

application.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"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="placeholderConfigurer1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="order" value="1"/><property name="ignoreUnresolvablePlaceholders" value="true"/><property name="locations"><list><value>classpath:app1.properties</value></list></property></bean><bean id="placeholderConfigurer2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="order" value="2"/><property name="locations"><list><value>classpath:app2.properties</value></list></property></bean><bean name="userBean" class="com.example.demoapi.test.UserBean"><property name="name" value="${app.name}"/></bean></beans>

app1.properties:

app.name=v1

app2.properties:

java">app.name=v2

userBean

java">public class UserBean {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}

改变优先级输出的不同:

在这里插入图片描述

PropertyPlaceholderConfigurer

类图如下:
在这里插入图片描述
可以看到是个BeanFactoryPostProcessor 且实现了顺序的;回顾ApplicationContext的refresh方法之invokeBeanFactoryPostProcessors,可以知晓执行这些BeanFactoryPostProcessor是有先后次序的。- - - 顺带复习:AnnotationConfigApplicationContext流程看@Configuration,@ComponentScan,@Import等的处理

placeholderConfigurer1 & placeholderConfigurer2的执行

上述placeholderConfigurer1,placeholderConfigurer2两个bean在invokeBeanFactoryPostProcessors的如下地方完成了实例化
在这里插入图片描述
这两个实例化出来的PropertyPlaceholderConfigurer接着排序
在这里插入图片描述

接着执行调用bfpp的postProcessBeanFactory方法
org.springframework.beans.factory.config.PropertyResourceConfigurer#postProcessBeanFactory
在这里插入图片描述
构造placeholderConfigurer2的PlaceholderResolvingStringValueResolver,然后进行beanDefinition合并操作

org.springframework.beans.factory.config.PlaceholderConfigurerSupport#doProcessProperties
在这里插入图片描述

userbean的BeanDefinition应用PropertyPlaceholderConfigurer前:

在这里插入图片描述

userbean的BeanDefinition应用PropertyPlaceholderConfigurer后:

在这里插入图片描述

因为PropertyPlaceholderConfigurer不同的order导致应用的先后不同,order更小的PropertyPlaceholderConfigurer先应用了;后面的应用就无效了

在这里插入图片描述

所以最后是PropertyPlaceholderConfigurer order更小的那个。

userBean的name的set

回顾bean的生命周期:https://doctording.blog.csdn.net/article/details/145044487

在userBean的属性设置populateBean阶段,获取到bean的所有PropertyValue,逐一设置

在这里插入图片描述

最后反射设置完成:
在这里插入图片描述

总结

  1. 普通bean应用PropertyPlaceholderConfigurer的时候,如果有多个PropertyPlaceholderConfigurer,应该要有顺序,属性相同的取order小的即高优先级的
  2. 具体是通过PropertyPlaceholderConfigurer改变BeanDefintion, 后续bean生命周期的populateBean阶段完成属性设置

http://www.ppmy.cn/devtools/156407.html

相关文章

neo4j初识

文章目录 一 图论基础二 柯尼斯堡七桥问题2.1 问题背景2.2 欧拉的解决3.1 核心概念3.2 核心优势3.3 应用场景3.4 技术特性3.5 版本与部署3.6 示例&#xff1a;社交关系查询3.7 限制与考量 四 图论与 Neo4j 的关联4.1 数据建模4.2 高效遍历4.3 应用场景 五 示例&#xff1a;用 N…

Node.js常用知识

Nodejs 总结Node.js基础知识&#xff0c;便于定期回顾 1、fs 文件写入 1、require(‘fs’) 2、fs.writeFile() 3、fs.appendFile() 4、fs.createwriteStream&#xff08;&#xff09; //流式写入 ws.write() 文件读取 1、fs.readFile(‘’,(err,data)>{ }) const …

前端知识速记—JS篇:箭头函数

前端知识速记—JS篇&#xff1a;箭头函数 什么是箭头函数&#xff1f; 箭头函数是 ES6 引入的一种新的函数书写方式&#xff0c;其语法更为简洁&#xff0c;常用于替代传统的函数表达式。箭头函数的基本语法如下&#xff1a; const functionName (parameters) > {// 函数…

HTML基本语法

什么是HTML? HTML是超文本标记语言&#xff08;HyperText Markup Language&#xff09;的缩写&#xff0c;是一种用于创建网页的标准标记语言。HTML允许网页设计师通过使用标签来描述网页的结构和内容。 W3C标准 W3C&#xff08;World Wide Web Consortium&#xff09;是一…

【Linux】Linux C判断两个IPv6地址是否有包含关系

功能说明 要判断两个 IPv6 地址是否具有包含关系&#xff0c;包括前缀的比较&#xff0c;可以通过以下步骤实现&#xff1a; 解析 IPv6 地址和前缀&#xff1a;将两个 IPv6 地址和它们的前缀长度解析为二进制形式。生成掩码&#xff1a;根据前缀长度生成掩码。按位比较&#…

计算机网络 IP 网络层 2 (重置版)

IP的简介&#xff1a; IP 地址是互联网协议地址&#xff08;Internet Protocol Address&#xff09;的简称&#xff0c;是分配给连接到互联网的设备的唯一标识符&#xff0c;用于在网络中定位和通信。 IP编制的历史阶段&#xff1a; 1&#xff0c;分类的IP地址&#xff1a; …

【漫话机器学习系列】077.范数惩罚是如何起作用的(How Norm Penalties Work)

范数惩罚的作用与原理 范数惩罚&#xff08;Norm Penalty&#xff09; 是一种常用于机器学习模型中的正则化技术&#xff0c;它的主要目的是控制模型复杂度&#xff0c;防止过拟合。通过对模型的参数进行惩罚&#xff08;即在损失函数中加入惩罚项&#xff09;&#xff0c;使得…

【贪心算法篇】:“贪心”之旅--算法练习题中的智慧与策略(二)

✨感谢您阅读本篇文章&#xff0c;文章内容是个人学习笔记的整理&#xff0c;如果哪里有误的话还请您指正噢✨ ✨ 个人主页&#xff1a;余辉zmh–CSDN博客 ✨ 文章所属专栏&#xff1a;贪心算法篇–CSDN博客 文章目录 前言例题1.买卖股票的最佳时机2.买卖股票的最佳时机23.k次取…