Spring 框架类PropertySourcesPlaceholderConfigurer

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的作用就是解析这些占位符,将它们替换为实际的属性值。
posted @ 2024-05-25 18:57  文采杰出  阅读(26)  评论(0编辑  收藏  举报