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. tf变换(1)

    TF库的目的是实现系统中任一个点在所有坐标系之间的坐标变换,也就是说,只要给定一个坐标系下的一个点的坐标,就能获得这个点在其他坐标系的坐标. 使用tf功能包,a. 监听tf变换: 接收并缓存系统中发布 ...

  2. eclipse如何设置高亮代码的背景色,比如选中某个单词,高亮所有的

    设置方法如下: 1.单击IDE顶部Window菜单下的Prefences,如图: 2.在打开对话框的左侧树上,找到Java节点下的Editor中的Mark Occurren,如图: 3.点击Mark ...

  3. 通过 Spark R 操作 Hive

    作为数据工程师,我日常用的主力语言是R,HiveQL,Java与Scala.R是非常适合做数据清洗的脚本语言,并且有非常好用的服务端IDE——RStudio Server:而用户日志主要储存在hive ...

  4. 【转】Mac使用apt-get

    和Linux系统不同的是,Mac系统默认是不带有apt-get软件包工具的,所以要想在Mac上使用上方便的apt-get就需要自己来安装它. 这里我们需要借助一个强大的工具-fink. 首先我们需要下 ...

  5. Java ArrayList源码剖析

    转自: Java ArrayList源码剖析 总体介绍 ArrayList实现了List接口,是顺序容器,即元素存放的数据与放进去的顺序相同,允许放入null元素,底层通过数组实现.除该类未实现同步外 ...

  6. 关于GO语言遇到illegal UTF-8 encoding 随手记录

    在使用汉字的时候会报错 解决方案 editpad++ 修改编码为UTF-8 保存就可以了~bingo

  7. FPGA内部动态可重置PLL讲解(二)

    对于全局时钟的管理,涉及到关于亚稳态的知识,大家可以上网搜索相关资料,这里不再赘述.亚稳态最简单的理解形式是无法判断是处于高电平状态还是处于低电平状态,这样会导致整个系统不稳定,会出现逻辑上的错误. ...

  8. Solr系列三:solr索引详解(Schema介绍、字段定义详解、Schema API 介绍)

    一.Schema介绍 1. Schema 是什么? Schema:模式,是集合/内核中字段的定义,让solr知道集合/内核包含哪些字段.字段的数据类型.字段该索引存储. 2. Schema 的定义方式 ...

  9. IntelliJ IDEA 2017 汉化包及教程

    一.准备 官网下载IntelliJ IDEA 2017 并安装好 下载汉化包 (百度云链接:http://pan.baidu.com/s/1slS9ZMP 密码:gp79) 二.汉化 此处有两种方法, ...

  10. unity-------Light的各个参数使用

    网上看到一篇关于灯光的文章,描述的很详细,如下: Lights will bring personality and flavor to your game. You use lights to il ...