首页 > 其他分享 >spring复习:(57)PropertyOverrideConfigurer用法及工作原理

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

时间:2023-11-10 12:31:57浏览次数:42  
标签:spring 57 xxx PropertyOverrideConfigurer dataSource DruidDataSource import com


一、属性配置文件

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/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://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 = new
                ClassPathXmlApplicationContext("spring6.xml");

        DruidDataSource dataSource = ctx.getBean("dataSource", DruidDataSource.class);
        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);
    }
}

xml文件中配置的属性值会被properties文件中的属性覆盖。

PropertyOverrideConfigurer父类是PropertyResourceConfigurer,它是一个BeanFactoryPostProcessor,所以,它的postProcessBeanFactory会被执行,这个方法代码如下:

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


这个方法首先获取到了属性值,然后调用processProperties方法(位于PropertyOverrideConfigurer类),这个方法代码如下:

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


可以看到它遍历Properties,然后调用了processKey方法,processKey的代码如下:

spring复习:(57)PropertyOverrideConfigurer用法及工作原理_后端_03


可以看到,调用了applyPropertyValue方法,传入了BeanFactory, BeanName, BeanProperty,value,

applyPropertyValue的代码如下:

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


可以看到,首先获取到了BeanDefinition,然后包装了一个PropertyValue,最后将包装的PropertyValue设置到了BeanDefinition.


标签:spring,57,xxx,PropertyOverrideConfigurer,dataSource,DruidDataSource,import,com
From: https://blog.51cto.com/amadeusliu/8295886

相关文章

  • Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理
    PropertySourcePlaceholderConfigurer的用途:通过配置文件(比如.properties文件)给bean设置属性,替代属性占位符示例:属性配置文件spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx/testspring.datasource.username=rootspring.datasource.password=xxxxxxspring.datasource.dri......
  • 【Spring】一次性打包学透 Spring | 阿Q送书第五期
    文章目录如何竭尽可能确保大家学透Spring1.内容全面且细致2.主题实用且本土化3.案例系统且完善4.知识有趣且深刻关于作者丁雪丰业内专家推图书热卖留言提前获赠书不知从何时开始,Spring这个词开始频繁地出现在Java服务端开发者的日常工作中,很多Java开发者从工作的第一天......
  • SpringBoot部署的jar包瘦身
    pom文件打包插件更换参考连接:https://www.jb51.net/program/293676eog.htm参考连接:https://blog.csdn.net/meng_9543/article/details/121329834<build><finalName>xxx-xxx</finalName><plugins><plugin>......
  • Spring BeanUtils.copyProperties简化写法
    代码importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;importorg.springframework.beans.BeanUtils;importorg.springframework.beans.BeansException;importorg.springframework.util.StopWatch;publicclassBeanUtils2{......
  • springboot学习日记(一)
    今天连下数据库,不小心打成netstartmysql了,好糗。。以后等时机到了笔记也该换成markdown写了,好久没写md后面得复习下。然后idea这边连数据库很简单不用写专门的程序,右侧栏database直接可以点开具体到连接某个数据库。记录一下注解的原理和作用:以前,『XML』是各大框架的青睐者,它......
  • Springboot报错,java.lang.IllegalArgumentException: argument type mismatch
    1、报错信息java.lang.IllegalArgumentException:argumenttypemismatch atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) atsun.reflect.DelegatingMethodAccessorI......
  • 「Java开发指南」如何用MyEclipse搭建Spring MVC应用程序?(二)
    本教程将指导开发者如何生成一个可运行的SpringMVC客户应用程序,该应用程序实现域模型的CRUD应用程序模式。在本教程中,您将学习如何:从数据库表的Scaffold到现有项目部署搭建的应用程序在上文中,我们介绍了如何创建一个Web项目和来自数据库表的Scaffold等,本文将继续介绍如何部......
  • 如何让spring一直启动不关掉
    Spring是J2EE应用程序框架,深受java程序员的欢迎,大量的项目都会引入Spring框架。如果是web项目,启动Spring之后,web容器会维持进程持续运行,Spring也就可以一直出于启动状态,但如果是普通的java应用,在启动Spring之后,随着java进程的停止,spring也会停止。那么如何让spring一直启动持续服......
  • SpringCloud 集成 Sentinel 和使用小结
    Sentinel是阿里的一款微服务请求监控组件,能够通过配置实现流量控制,降级熔断,热点参数限流,授权规则限流,使用非常方便。官方默认只提供了流量控制配置规则持久化代码实现,这也是我们最常用的,上篇博客已经实现并进行了部署。本篇博客基于上篇博客搭建好的Sentinel和Nacos环境,介绍......
  • springboot社区团购管理系统的设计与实现-计算机毕业设计源码+LW文档
    abo开发说明开发语言:Java框架:springbootJDK版本:JDK1.8服务器:tomcat7数据库:mysql5.7(一定要5.7版本)数据库工具:Navicat11开发软件:eclipse/myeclipse/ideaMaven包:Maven3.3.9浏览器:谷歌浏览器部分数据库:----Tablestructurefortableaddress--DROPTABLEIFEXISTSaddr......