一、在 Spring Config 文件中配置 Bean 时,有时候需要在 Bean 的配置里添加 系统部署的细节信息, 如文件路径,数据源配置信息。而这些部署细节实际上需要在配置文件外部来定义。

二、Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器。这个处理器允许用户将 Bean 的配置部分内容外移到属性文件中,然后可以在 Bean 的配置文件

里使用形式为 ${var}的变量,PropertyPlaceholderConfigurer 从属性文件里加载属性,并使用这些属性来替换变量。

三、Spring 还允许在属性文件中使用 ${key},以属性间的互相引用。

四、使用:需要注册 PropertyPlaceholderConfigurer 。通过 <context:property-placeholder location="props.properties"/> 这种方式来指定属性文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"/>
<property name="password" value="lgh123"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?user=root&amp;password=lgh123&amp;useUnicode=true&amp;characterEncoding=UTF8&amp;useSSL=true"/>
</bean>--> <!--导入属性文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--使用外部的属性文件的属性配置-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
</bean>
</beans>

db.properties

user=root
password=lgh123
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/test?user=root&amp;password=lgh123&amp;useUnicode=true&amp;characterEncoding=UTF8&amp;useSSL=true

测试:

package com.xiya.spring.beans.properties;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.sql.DataSource;
import java.sql.SQLException; /**
* Created by N3verL4nd on 2017/3/22.
*/
public class Main {
public static void main(String[] args) throws SQLException {
ApplicationContext context = new ClassPathXmlApplicationContext("beans-properties.xml"); DataSource dataSource = (DataSource) context.getBean("dataSource"); System.out.println(dataSource.getConnection());
}
}

Spring使用外部属性文件的更多相关文章

  1. spring 使用外部属性文件

    一.PropertyPlaceholderConfigurer spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件. Proper ...

  2. Spring 使用外部属性文件配置

    1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...

  3. Spring 应用外部属性文件 配置 context 错误

    在Spring配置文件中出现通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明这个错误,其实主要是我们在引入命名空间时没有正确引入它的DTD解析 ...

  4. Spring_Bean的作用域---和使用外部属性文件

    <!-- 使用 bean的scope属性来配置bean的作用域 singleton:默认值.容器初始时创建bean实例,在整个容器的生命周期内只创建这一个bean单例 prototype:原型的 ...

  5. Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...

  6. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件

    Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...

  8. Spring基础12——使用外部属性文件

    1.使用外部属性文件 在配置文件里配置Bean时,有时需要在Bean的配置文件里引入系统部署的细节信息(例如:文件的路径.数据源配置信息等),而这些部署细节实际上需要和bean配置相分离,因为我们修改 ...

  9. 十八 Spring的JDBC模板:引入外部属性文件

    配置外部属性文件 配置文件里引入属性文件,两种方式 第一种: 第二种: 引入属性文件的值: 测试: <?xml version="1.0" encoding="UT ...

随机推荐

  1. 记一次 爬取LOL全皮肤原画保存到本地的实例

    #爬取lol全英雄皮肤 import re import traceback # 异常跟踪 import requests from bs4 import BeautifulSoup #获取html ...

  2. 使用 Python 中 re 模块对测试用例参数化,进行搜索 search、替换 sub

    自动化测试用例,如果一百个接口要在Excel写100个sheet表单,每个接口有10个字段,里面有5个都可能是变化的,需要使用参数化,先试用特定的字符进行站位,在构造参数时在进行替换占位符: 一.用力 ...

  3. Python+appium+unittest UI自动化测试

    什么是UI自动化 自动化分层 单元自动化测试,指对软件中最小可测试单元进行检查和验证,一般需要借助单元测试框架,如java的JUnit,python的unittest等 接口自动化测试,主要检查验证模 ...

  4. 为云而生,腾讯云服务器操作系统TencentOS内核正式开源

    ​1月9日,腾讯云宣布将开源其服务器操作系统TencentOS内核.相比业内其它版本Linux 内核,腾讯云 TencentOS 内核在资源调度弹性.容器支持.系统性能及安全等层面极具竞争力,特别适合 ...

  5. Python保存时提示“SyntaxError: Non-ASCII character '\xe8' in file”

    原因 Python 默认的是ASCII 编码方式,如果出现中文会出现问题,所哟必须在代码的第二行或第一行 显示的声明编码方式(已注释的方式,即"#"开始) 解决方法 在文件头部加上 ...

  6. 欧拉-拉格朗日方程 The Euler-Lagrange Equation

    在 paper: Bounded Biharmonic Weights for Real-Time Deformation 中第一次接触到 Euler-Lagrange 方程,简单记录一下. 泛函的定 ...

  7. Your Ride Is Here 你的飞碟在这儿 USACO 模拟

    1001: 1.1.1 Your Ride Is Here 你的飞碟在这儿 时间限制: 1 Sec  内存限制: 128 MB提交: 9  解决: 9[提交] [状态] [讨论版] [命题人:外部导入 ...

  8. cf - 920 c 求能否实现交换

    C. Swap Adjacent Elements time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. OpenGLES思维导图

    两本书到头来就只剩下了这三张图了吧.想要原图:https://github.com/wangwangla/biji/blob/master/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8 ...

  10. UVA540 Team Queue——题解 by hyl天梦

    UVA540 Team Queue 题解 题目描述:题目原题 https://vjudge.net/problem/UVA-540 Queues and Priority Queues are dat ...