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

spring复习:(57)PropertyOverrideConfigurer用法及工作原理

一、属性配置文件

dataSource.url=jdbc:mysql://xxx.xxx.xxx.xxx/test
dataSource.username=root
dataSource.password=xxxxxx
dataSource.driverClassName=com.mysql.jdbc.Driver
#dataSource.type=com.alibaba.druid.pool.DruidDataSource

二、spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"><property name="locations" value="classpath:cn/edu/tju/jdbc2.properties"/></bean><bean id="dataSource"class="com.alibaba.druid.pool.DruidDataSource"></bean></beans>

三、测试类:

package cn.edu.tju.test;import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.sql.SQLException;public class Test06 {public static void main(String[] args) throws Exception {ConfigurableApplicationContext ctx = newClassPathXmlApplicationContext("spring6.xml");DruidDataSource dataSource = ctx.getBean("dataSource", DruidDataSource.class);DruidPooledConnection connection = dataSource.getConnection();System.out.println(connection);}
}

xml文件中配置的属性值会被properties文件中的属性覆盖。
PropertyOverrideConfigurer父类是PropertyResourceConfigurer,它是一个BeanFactoryPostProcessor,所以,它的postProcessBeanFactory会被执行,这个方法代码如下:
在这里插入图片描述
这个方法首先获取到了属性值,然后调用processProperties方法(位于PropertyOverrideConfigurer类),这个方法代码如下:
在这里插入图片描述
可以看到它遍历Properties,然后调用了processKey方法,processKey的代码如下:
在这里插入图片描述
可以看到,调用了applyPropertyValue方法,传入了BeanFactory, BeanName, BeanProperty,value,
applyPropertyValue的代码如下:
在这里插入图片描述
可以看到,首先获取到了BeanDefinition,然后包装了一个PropertyValue,最后将包装的PropertyValue设置到了BeanDefinition.

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

相关文章:

  • 在axios中获取文件上传进度
  • 黑马头条-kafka配置
  • PMP P-01 Basic Knowledge
  • java八股文面试[数据结构]——ArrayList和LinkedList区别
  • OpenCV中QR二维码的生成与识别(CIS摄像头解析)
  • 常见排序集锦-C语言实现数据结构
  • css 实现四角边框样式
  • 机器学习深度学习——自注意力和位置编码(数学推导+代码实现)
  • 02.案列项目Demo
  • PDF校对:追求文档的精准与完美
  • 低代码解放生产力,助力企业高效发展
  • 【前端从0开始】CSS——9、浮动
  • 如何在Moonriver网络上向社区代表委托投票权利
  • 时序预测 | MATLAB实现基于CNN-GRU卷积门控循环单元的时间序列预测-递归预测未来(多指标评价)
  • 【李群李代数】李群控制器(lie-group-controllers)介绍——控制 SO(3) 空间中的系统的比例控制器Demo...
  • DP读书:鲲鹏处理器 架构与编程(六)PCI Express 总线
  • Pyqt5-开源工具分解功能(文本拖拽)
  • Java版B/S架构 智慧工地源码,PC、移动、数据可视化智慧大屏端源码
  • 无涯教程-PHP - Session选项
  • The Age of Data and AI: Challenges and Opportunities
  • WPF 项目中 MVVM模式 的简单例子说明
  • 基于nginx禁用访问ip
  • 【第三阶段】kotlin语言的内置函数let
  • 【C++入门到精通】C++入门 —— 模版(template)
  • ARM汇编【3】:LOAD/STORE MULTIPLE PUSH AND POP
  • Python之Qt输出UI
  • 【1day】复现泛微OA某版本SQL注入漏洞
  • 安卓系列机型-禁止卸载某个APP 防止误卸载软件 无需root权限
  • 【算法系列篇】二分查找——这还是你所知道的二分查找算法吗?
  • 【前端从0开始】JavaSript——分支流程控制