1.PropertyPlaceholderConfigurer类
它是把属性中的定义的变量(var)替代,spring的配置文件中使用${var}的占位符

<beans>
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
           <property name="location"><value>db.properties</value></property>
</bean> 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
    <property name="url"><value>${jdbc.url}</value></property>
    <property name="username"><value>${jdbc.username}</value></property>
    <property name="password"><value>${jdbc.password}</value></property>
</bean>
</beans>

db.properties文件

jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root

2.PropertyOverrideConfigurer类
跟PropertyPlaceholderConfigurer功能一样,不过用法不一样.不用占位符,在属性文件中
直接定义属性的值,这样就允许有默认值

<beans>
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
           <property name="location"><value>db.properties</value></property>
</bean> 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
    <property name="url"><value>jdbc:hsqldb:hsql://production:9002</value></property>
    <property name="username"><value>test</value></property>
    <property name="password"><value>123456</value></property>
</bean>
</beans>

db.properties文件

dataSource.username=admin
dataSource.password=9527

在bean实例时,admin,9527将替代test,123456
3其他
1)如果需要引用多个属性,将configBean属性改为

<property name="locations">
<list>
<value>db.properties</value>
<value>db1.properties</value>
</list>
</property>

2)在ApplactionContext中是自动调用BeanFactoryPostProcessor接口的,如果要在BeanFactory中使用,必须手动添加:

XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("jdbc.properties"));
cfg.postProcessBeanFactory(factory);

spring如何引用properties文件里的配置的更多相关文章

  1. Spring如何引用properties文件里的…

    [zt]Spring如何引用properties文件里的配置 来自 http://blog.csdn.net/luobo525/archive/2006/11/06/1370258.aspx 1.Pr ...

  2. 【Spring MVC】Properties文件的加载

    [Spring MVC]Properties文件的加载 转载:https://www.cnblogs.com/yangchongxing/p/10726885.html 参考:https://java ...

  3. 【Quartz】Spring Boot使用properties文件配置Quartz

    (1)在resource目录下新建quartz.properties文件 #============================================================== ...

  4. spring boot 在框架中注入properties文件里的值(Spring三)

    前一篇博客实现了打开第一个页面 链接:https://blog.csdn.net/qq_38175040/article/details/105709758 本篇博客实现在框架中注入propertie ...

  5. spring配置文件引入properties文件:<context:property-placeholder>标签使用总结

    一.问题描述: 1.有些参数在某些阶段中是常量,比如: (1)在开发阶段我们连接数据库时的连接url.username.password.driverClass等 (2)分布式应用中client端访问 ...

  6. Spring 中使用Properties文件

    Spring提供了加载Properties文件的工具类:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer. ...

  7. 曹工说Spring Boot源码(6)-- Spring怎么从xml文件里解析bean的

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  8. Spring自动注入properties文件

    实现spring 自动注入属性文件中的key-value. 1.在applicationContext.xml配置文件中,引入<util />命名空间. xmlns:util=" ...

  9. Spring加载properties文件的属性的值

    要使用配置文件的值首先在spring.xml配置加载properties文件 <context:property-placeholder location="classpath:ife ...

随机推荐

  1. Linux C语言小程序

    Linux C语言小程序 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include & ...

  2. C#修改GIF大小同时保持GIF仍然可动和背景透明

    /// <summary> /// 设置GIF大小 /// </summary> /// <param name="path">图片路径< ...

  3. WebForm中使用MVC

    http://www.cnblogs.com/encoding/articles/3556046.html ********************************************** ...

  4. Python以不可见字符作为列分割符

    # -*- coding: utf-8 -*- import sys import time CTRL_A='\x01' CTRL_B='\x02' thedate = '' thetime = '' ...

  5. Gson json解析工具

    json 解析工具 ,谷歌出品 对象转换字符串 HashMap<String,String> hashMap = new HashMap<String, String>();  ...

  6. shadowshocks下载地址

    https://github.com/shadowsocks/shadowsocks-windows/releases

  7. .NetCore简介

    引用:https://docs.microsoft.com/zh-cn/dotnet/articles/core/index .NET Core 是一个通用开发平台,由 Microsoft 和 Git ...

  8. 一款纯css3实现的翻转按钮

    之前为大家介绍了好多纯css3实现的很漂亮的按钮.今天小编要给各网友再分享一款纯css3实现的翻转按钮.实现中给出了两种的翻转特效,一种是基于按扭的左边缘为中心线,另一种是基于按钮的中间为中心线.我们 ...

  9. SVN回滚至某个版本

    今天新上传的版本出错了,需要回滚至之前的版本. 记录一下过程. 在项目文件夹下. 1.打开日志历史.右键单击,TortoiseSVN-->show log 2.要回滚至版本x.在版本x上右键单击 ...

  10. FusionCharts JavaScript API - Functions 常用方法整理笔记

    FusionCharts JavaScript API - Functions Home > FusionCharts XT and JavaScript > API Reference  ...