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. hdoj 3488 Tour 【最小费用最大流】【KM算法】

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submi ...

  2. C#之打印乘法表

    设计说明 由图可知: 1.我们需要打印出九行: 2.每行中最大列数等于行数: 代码实现 public void Display1() { Console.WriteLine("乘法表:&qu ...

  3. 第一个关于selenium项目

    1.创建一个简单的Python工程 在主菜单中,选择File | New Project ,并指定Python解释器版本 2.创建python类,快捷键alt+insert 3.编写打开浏览器的代码, ...

  4. ORA-09925: Unable to create audit trail file汇总

    今天一兄弟的库报ORA-09925: Unable to create audit trail file,当时查 df -h有可用空间,文件夹的权限也正确,未df -i查看Inodes使用情况,审计文 ...

  5. ZOJ 3829 Known Notation(字符串处理 数学 牡丹江现场赛)

    题目链接:problemId=5383">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Do you ...

  6. 关联查询之map的延伸使用方法

    <select id="front.sort.selectListall" parameterType="myshop.services.front.sort.be ...

  7. Configure environment variables for different tools in jenkins

    安装以下的工具,并在Jenkins中的Manage Jenkins-->Configure System-->Global Properties-->Environment Vari ...

  8. 院校-国外-美国:斯坦福大学( Stanford)

    ylbtech-院校-国外-美国:斯坦福大学( Stanford) 斯坦福大学(Stanford University),全名小利兰·斯坦福大学(Leland Stanford Junior Univ ...

  9. 计算机网络自顶向下方法第2章-应用层(application-layer).2

    2.4 DNS:因特网的目录服务 2.4.1 DNS提供的服务 DNS的定义 实体层面看,DNS是一个由分层的DNS服务器实现的分布式数据库 协议层面看,DNS是一个使得主机能够查询分布式数据库的应用 ...

  10. css3 animate基本属性

    Css3animate属性 属性 描述 Css Animation 所有动画属性的简写属性,除了animation-play-state属性 Animation:name duration timin ...