1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容

移到属性文件中。可以在Bean配置文件使用${var}的变量,PropertyPlaceholderConfigurer从属性文件中加载属性。

一般 属性文件后点缀properties

案例:数据源的配置(配置链接数据库的信息)

处理spring 的必须包之外,我们还要导入两个架包

1.数据库的的驱动包

2.C3p0的架包

db-properties.xml

<?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-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
 
 
      <!-- 导入属性文件   -->  
    <context:property-placeholder location="classpath:db.properties"/>

  <!-- 数据库链接池配置 -->
      <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
            <property name="user" value="${user}"></property>  
            <property name="password" value="${password}"></property>  
            <property name="driverClass" value="${driverClass}"></property>  
            <property name="jdbcUrl" value="${jdbcUrl}"></property>  
     </bean>  
</beans>

<!--外部属性文件--->

db.properties
user=root
password=sys123456
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///xbc

写一个测试方法Main,测试是否成功链接数据库

package com.spring.db;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Dbtest {

public static void main(String[] args) throws SQLException {
        ApplicationContext  ctx=new ClassPathXmlApplicationContext("db-properties.xml");
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        System.out.println("执行中");
        System.out.println(dataSource.getConnection());
        System.out.println("结束");
    }


   
         

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

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

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

  2. 使用外部属性文件配置Bean以及Bean的生命周期方法

    1.使用外部属性文件配置Bean 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean ...

  3. spring 使用外部属性文件

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

  4. Spring使用外部属性文件

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

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

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

  6. IoC容器-Bean管理XML方式(引入外部属性文件)

    IoC操作Bean管理(引入外部属性文件) 1,直接配置数据库信息 (1)配置德鲁伊连接池 (2)引入德鲁伊连接池依赖jar包 2,通过引入外部属性文件配置数据库连接池 (1)创建外部属性文件,pro ...

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

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

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

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

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

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

随机推荐

  1. Java设计模式菜鸟系列(十五)建造者模式建模与实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39863125 建造者模式(Builder):工厂类模式提供的是创建单个类的模式.而建造者模 ...

  2. git的经常使用命令

    $ git config --global user.name "姓名" $ git config --global user.email "xxx@qq.com&quo ...

  3. sikuli类、函数使用参考java doc

    sikuli类.函数使用可以参考java  dochttp://doc.sikuli.org/javadoc/ http://stackoverflow.com/questions/9568612/s ...

  4. Android——build.prop 解析【转】

    本文转载自:http://blog.csdn.net/lengyue1084/article/details/77637354 一.概念 在Android设备shell终端可以看到/system目录下 ...

  5. bzoj1081: [SCOI2005]超级格雷码(dfs)

    1081: [SCOI2005]超级格雷码 题目:传送门 题解: 又是一道水题... 因为之前做过所以知道规律: 如n=2 B=3: 00 10 20    21 11 01    02 12 22 ...

  6. Java Colections 集合类 —— List、ArrayList、Set(HashSet)

    0. List<T> 是一个接口 该接口定义的高级成员函数有: contains() ⇒ 是否包含: String[] people; List names = Arrays.asList ...

  7. 排序系列 之 冒泡排序及其改进算法 —— Java实现

    冒泡排序算法 冒泡排序算法 改进一 冒泡排序算法 改进二 冒泡排序算法 改进三 冒泡排序算法 基本思想: 在要排序的一组数中,对当前还未排好序的范围内的全部数据,自上而下对相邻的两个数依次进行比较和调 ...

  8. Django之缓存机制

    1.1 缓存介绍 1.缓存的简介 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一次 ...

  9. SilverLight Q&A

    1.在学校prism,Unity框架的时候,遇到的问题“The IModuleCatalog is required and cannot be null in order to initialize ...

  10. 免费获取Bootstrap模板的方法

    Bootstrap是Twitter推出的一个开源的用于前端开发的工具包,其中中包含了丰富的Web组件,根据这些组件,可以快速的搭建一个漂亮.功能完备的网站. 最近通过了Bootstrap中文网学习了其 ...