8.1.4 属性占位符配置器

        PropertyPlaceholderConfigurer 是一个容器后处理器,负责读取Properties属性文件里的属性值,并将这些属性值设置成Spring配置文件的数据。

        通过使用PropertyPlaceholderConfigurer后处理器,可以将Spring配置文件中的部分数据放在属性文件中设置。

        XML :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>dbconn.properties</value>
<!-- 如果有多个属性文件,依次在下面列出来 -->
<!-- <value>wawa.properties</value> -->
</list>
</property>
</bean> <!-- 对于采用基于XML Schema的配置文件而言,如果导入了context:命名空间,则可采用如下方式来配置该属性占位符 -->
<!-- <context:property-placeholder location="classpath:wawa.properties"/> --> <!-- 定义数据源Bean,使用C3P0数据源实现 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- Spring容器将从PropertyPlaceholderConfigurer指定的属性文件中搜索这些key对应的value,并为该Bean的属性值设置这些value值 -->
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> </beans>

        Properties :

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=system

        Class : Main

package edu.pri.lime._8_1_4.main;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException; import javax.sql.DataSource; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("app_8_1_4.xml");
DataSource ds = null;
PreparedStatement pstmt = null;
Connection conn = null;
try {
ds = ctx.getBean("dataSource",DataSource.class);
conn = ds.getConnection();
pstmt = conn.prepareStatement("insert into news_inf values(null,?,?)");
pstmt.setString(1, "lime");
pstmt.setString(2, "Oracle");
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(pstmt != null)
try {
pstmt.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(conn != null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

啦啦啦

8 -- 深入使用Spring -- 1...4 属性占位符配置器的更多相关文章

  1. 8 -- 深入使用Spring -- 1...4 重写占位符配置器

    8.1.5 重写占位符配置器 (PropertyOverrideConfigurer) PropertyOverrideConfigurer是Spring提供的另一个容器后处理器.PropertyOv ...

  2. spring4笔记----PropertyPlaceholderConfigurer 属性占位符配置器

    driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/spring username=root password= ...

  3. Spring - IoC(12): 属性占位符

    使用属性占位符可以将 Spring 配置文件中的部分元数据放在属性文件中设置,这样可以将相似的配置(如 JDBC 的参数配置)放在特定的属性文件中,如果只需要修改这部分配置,则无需修改 Spring ...

  4. spring4笔记----PropertyOverrideConfigureer 重写占位符配置器(图)

  5. Spring(3.2.3) - Beans(12): 属性占位符

    使用属性占位符可以将 Spring 配置文件中的部分元数据放在属性文件中设置,这样可以将相似的配置(如 JDBC 的参数配置)放在特定的属性文件中,如果只需要修改这部分配置,则无需修改 Spring ...

  6. Spring属性占位符 PropertyPlaceholderConfigurer

    http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html PropertyPlaceholderConfigurer是个bean工厂后 ...

  7. Spring实战(八)bean装配的运行时值注入——属性占位符和SpEL

    前面涉及到依赖注入,我们一般哦都是将一个bean引用注入到另一个bean 的属性or构造器参数or Setter参数,即将为一个对象与另一个对象进行关联. bean装配的另一个方面是指将一个值注入到b ...

  8. SpringBoot环境属性占位符解析和类型转换

    前提 前面写过一篇关于Environment属性加载的源码分析和扩展,里面提到属性的占位符解析和类型转换是相对复杂的,这篇文章就是要分析和解读这两个复杂的问题.关于这两个问题,选用一个比较复杂的参数处 ...

  9. spring占位符解析器---PropertyPlaceholderHelper

    一.PropertyPlaceholderHelper 职责 扮演者占位符解析器的角色,专门用来负责解析路劲中or名字中的占位符的字符,并替换上具体的值 二.例子 public class Prope ...

随机推荐

  1. 【C】——可变参数

    写代码之前要先介绍一下可变参数的备用知识: C函数要在程序中用到以下这些宏: void va_start( va_list arg_ptr, prev_param ); type va_arg( va ...

  2. PCL采样一致性算法

    在计算机视觉领域广泛的使用各种不同的采样一致性参数估计算法用于排除错误的样本,样本不同对应的应用不同,例如剔除错误的配准点对,分割出处在模型上的点集,PCL中以随机采样一致性算法(RANSAC)为核心 ...

  3. OC与AS3的观察者模式比较以及外部回调

    一.要点 1.AS3的观察者模式,主要是体现在在哪个组件上监听,那么就在这个组件上分发事件,并且可以直接传递Function. 2.OC的观察者模式,主要是需要你指定观察的对象,和观察的对象方法sel ...

  4. sourcetree向github推送代码提示密码错误

    明明用户名和密码都是正确,sourceTree却在推送代码的时候提示无效的用户名或密码. 1.设置 2.选中并编辑 3.如果你本来选择的就是 GitHub,或者选择完以后还是不行的话,在 URL / ...

  5. webpack1--安装与初始化

    webpack可用于打包所有的脚本,图片,样式表及其他资源. 今天来讲一下如何安装webpack 及 创建package.json.webpack依赖于npm和 node,所以本文是假设你已安装好了n ...

  6. node学习笔记2——搭建服务器

    搭建服务器关键词: require('http') —— 请求 node 里面的 http 模块 createServer —— 创建一个服务器 request —— 请求 response—— 响应 ...

  7. 自然语言交流系统 phxnet团队 创新实训 项目博客 (十四)

    项目技术借鉴报告: 一.服务器端(Server) 1.分词 分词使用的是庖丁分词.使用盘古分词词库和词素作为辅助.其中包括下载Jar包并且使用. 2.人工神经网络 以网上已有的初级网络为蓝图,结合机器 ...

  8. 关于Unity中顶点片元Shader实例

    补充 float4 fixed4 _Time 1: float4是内置向量 (x, y, z, w); float4 a; 访问单独成员a.x, a.y, a.z, a.w;2: fixed4 是内置 ...

  9. SQL Server 连接远程服务器

    最近要用到sqlserver,将本地的数据更新到远端的sqlserver时,希望能够查看远端sqlserver数据变化. 下载Microsoft SQL Server Management Studi ...

  10. (转)【多媒体封装格式详解】--- AAC ADTS格式分析

     出自:http://blog.csdn.net/tx3344/article/details/7414543 http://www.it6655.com/2012/08/aac-adts-html ...