首页 > 其他分享 >Spring 框架类PropertySourcesPlaceholderConfigurer

Spring 框架类PropertySourcesPlaceholderConfigurer

时间:2024-05-25 19:14:15浏览次数:11  
标签:username String 框架 url Spring driver PropertySourcesPlaceholderConfigurer passwo

PropertyOverrideConfigurer 是 Spring 框架中的一个类,它允许你在 Spring 的配置文件之外通过外部属性文件来覆盖已定义的 bean 属性。这在部署不同的环境(如开发、测试、生产)时特别有用,因为你可以为不同的环境定义不同的属性,而无需修改 Spring 的配置文件。
演示:

  • 创建实体类:
package com.powernode.spring6.bean;
import javax.sql.DataSource;

public class MyDataSource  {
    private String driver;
    private String url;
    private String username;
    private String password;

    @Override
    public String toString() {
        return "MyDataSource{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

/*    public String getDriver() {
        return driver;
    }

    public String getUrl() {
        return url;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }*/
}
  • 在resources目录下创建外部配置文件dao.properties
dataSource.driver=com.mysql.cj.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/mydb_dev
dataSource.username=root
dataSource.password=123

此处dao.properties中的内容必须以<bean id="dataSource"为前缀;id值如果变了,此处的前缀也得跟着变.

  • 编辑spring配置文件
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--通过location引入外部配置文件-->
    <bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
        <property name="location" value="classpath:dao.properties"/>
    </bean>
    <bean id="dataSource" class="com.powernode.spring6.bean.MyDataSource">
        <property name="username" value="haha"/>
        <property name="driver" value="baidu.com"/>
        <property name="url" value="qq.com"/>
        <property name="password" value="456"/>
    </bean>
</beans>
  • 测试
    @Test
    public void TestProperties(){
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring.properties.xml");
        MyDataSource myDataSource = classPathXmlApplicationContext.getBean("dataSource", MyDataSource.class);
        System.out.println(myDataSource);
    }

输出结果为:MyDataSource{driver='com.mysql.cj.jdbc.Driver', url='jdbc:mysql://localhost:3306/mydb_dev', username='root', password='123'}
也就是覆盖了spring配置文件中原来的配置.

  • 总结:
  1. PropertyOverrideConfigurer主要用于覆盖已定义的bean属性。当你在Spring配置文件中定义了一个bean的属性,但希望在运行时通过外部属性文件来动态覆盖这些属性的值时,你可以使用PropertyOverrideConfigurer。它允许你通过外部属性文件来灵活地修改bean的属性,而无需修改Spring配置文件本身。
  2. PropertySourcesPlaceholderConfigurer则主要用于处理占位符。在Spring配置文件中,你可能会使用一些占位符(如${property.name})来表示一些属性值,而这些占位符的具体值希望在运行时从外部属性文件或其他来源中获取。PropertySourcesPlaceholderConfigurer的作用就是解析这些占位符,将它们替换为实际的属性值。

标签:username,String,框架,url,Spring,driver,PropertySourcesPlaceholderConfigurer,passwo
From: https://www.cnblogs.com/DuWenjie/p/18212847

相关文章

  • springboot3.0+shardingsphere5.2 最简单的分库分表
    先看表结构两个数据库test1,test2每个库有4张sys_user表分别命名sys_user_0-4maven依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>......
  • 基于python+django框架旅游景区景点购票系统设计与实现(源码+LW+安装+基础课)
     博主介绍:黄菊华老师《Vue.js入门与商城开发实战》《微信小程序商城开发》图书作者,CSDN博客专家,在线教育专家,CSDN钻石讲师;专注大学生毕业设计教育和辅导。所有项目都配有从入门到精通的基础知识视频课程,学习后应对毕业设计答辩。项目配有对应开发文档、开题报告、任务书、P......
  • spring boot 的常用注解
    Autowired与Resource的区别1、Autowired与Resource都是要装配beanAutowired是通过byType来注入Resource是同通过byName来注入一、AutowiredAutowired默认是按类型(type也就是参数化类型)装配(这个注解是属于spring的),默认情况下Autowired要求依赖对象必须存在,如果想要允许他......
  • SpringBoot自动装配原理是什么?
    1创建上下文publicConfigurableApplicationContextrun(String...args){//记录程序运行时间StopWatchstopWatch=newStopWatch();stopWatch.start();//ConfigurableApplicationContextSpring的上下文ConfigurableAppl......
  • gin框架模板渲染
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录gin框架模板渲染自定义模板函数静态文件处理gin框架模板渲染这个目录的路径要放正确(虽然我也不知道为什么突然就解决了)==错误模板====正确版本==packagemainimport( "net/http"......
  • Spring框架篇常考八股
    1.什么是Ioc和DIIoC不是一种技术,只是一种思想,一个重要的面向对象编程的法则,它能指导我们如何设计出松耦合、更优良的程序。传统应用程序都是由我们在类内部主动创建依赖对象,从而导致类与类之间高耦合,难于测试;有了IoC容器后,把创建和查找依赖对象的控制权交给了容器,由容器......
  • 49.SpringMVC简单整理
    SpringMVC本质也是解耦先提前插一嘴这里用到的是简单的maven项目:在添加web应用时报错ArtifactsWebfacetresources爆红参考:https://blog.csdn.net/weixin_43739266/article/details/122133398添加web依赖要为项目添加web项目依赖否则无法tomcat访问为了介绍MVC......
  • Spring的BeanFactoryPostProcessor和BeanPostProcessor区别?
    1、BeanFactoryPostProcessor是用于在容器实例化Bean之前对Bean的配置信息进行修改的接口,它允许对BeanFactory进行后处理,比如修改BeanDefinition的属性值等。BeanFactoryPostProcessor是在Bean的实例化之前执行的,对BeanFactory进行配置。2、BeanPostProcessor是用于在Bean实......
  • Springboot计算机毕业设计信息学院网络工程党支部小程序【附源码】开题+论文+mysql+程
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景在信息化高速发展的今天,党建工作也需要与时俱进,利用信息技术手段提升工作效率和影响力。信息学院网络工程党支部作为学院党建工作的前沿阵地,面临着如......
  • 【精品毕设推荐】基于Springboot的学生心理咨询评估系统的设计与实现
    点击下载原文及代码,可辅助在本地配置运行摘 要使用旧方法对学生心理咨询评估信息进行系统化管理已经不再让人们信赖了,把现在的网络信息技术运用在学生心理咨询评估信息的管理上面可以解决许多信息管理上面的难题,比如处理数据时间很长,数据存在错误不能及时纠正等问题。这次......