博客地址http://www.cnblogs.com/shizhongtao/p/3438431.html

属性文件命名是*.properties,在java中,用类java.util.Properties来处理,例如我创建文件my.properties,然后加入属性

filename=你好
path=D\:\\app

那么在java中就可以这样读取属性

InputStream in=App.class.getResourceAsStream ("/my.properties");
Properties pro=new Properties();
try
{
pro.load(in);
System.out.println(pro.getProperty("filename"));
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(in!=null){
try
{
in.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

上面代码中只是读取了,其中的filename属性,不过会出现一个问题,就是中文乱码的问题,这是因为inputstream是按照字节流读取,每次读一个字节,而每个汉字是两个字节;所以会出现乱码,我们可以这样修改,(当然你也可以用fileReader这个类,因为FileREder用的是系统默认字符编码,只要你默认编码支持中文就可以了。)

new InputStreamReader(in,"utf-8");
pro.load(new InputStreamReader(in,"utf-8"));
System.out.println(pro.getProperty("filename"));

在spring中,利用配置文件读取相应的配置,方便项目一些变量的修改,例如数据库配置、文件保存位置、类中不固定的一些变量值等等。

通常我们会建一个名字为db.propreties的属性文件。里面内容如下:

db.connection.driver_class=oracle.jdbc.driver.OracleDriver
db.connection.url=jdbc\:oracle\:thin\:@localhost\:\:orcl
db.connection.username=test
db.connection.password=test

然后在spring中用PropertyPlaceholderConfigurer类来读取属性,这个类读取属性文件非常灵活,你可以配置编码 <property name="fileEncoding">来配置编码。具体使用可以参看官方的文档http://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-framework-reference/htmlsingle/#beans-factory-placeholderconfigurer

 <bean id="propertyConfigurer"        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/db.properties</value>
</list>
</property>
</bean>

在spring2.5以后的版本中,读取属性文件也可以这样配置,与上面的效果一样。

<context:property-placeholder location="classpath:db.properties"/>

这样在我们配置数据库的dataSource时候就可以如下配置:

      class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${db.connection.driver_class}</value>
</property>
<property name="url">
<value>${db.connection.url}</value>
</property>
<property name="username">
<value>${db.connection.username}</value>
</property>
<property name="password">
<value>${db.connection.password}</value>
</property>
</bean>

引申:如果想要PropertyPlaceholderConfigurer用anotation的方式,去注入属性,我们该如何写呢?

spring读取prperties配置文件(2)

spring读取prperties配置文件(1)的更多相关文章

  1. spring读取prperties配置文件(2)

    接上篇,spring读取prperties配置文件(1),这一篇主要讲述spring如何用annotation的方式去读取自定义的配置文件. 这里我先定义好属性文件"user.propert ...

  2. Spring读取xml配置文件的原理与实现

    本篇博文的目录: 一:前言 二:spring的配置文件 三:依赖的第三方库.使用技术.代码布局 四:Document实现 五:获取Element的实现 六:解析Element元素 七:Bean创造器 ...

  3. spring读取xml配置文件(二)

    一.当spring解析完配置文件名的占位符后,就开始refresh容器 @Override public void refresh() throws BeansException, IllegalSt ...

  4. spring 读取yaml配置文件

    从Spring框架4.1.0增加了对YAML的支持,Spring框架4.1.0 maven POM具有Snakeyaml依赖性  . 您可以在Spring Boot应用中使用两种方式加载YAML: 1 ...

  5. Spring 读取XML配置文件的两种方式

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

  6. Java中spring读取配置文件的几种方法

    Spring读取配置XML文件分三步: 一.新建一个Java Bean: package springdemo; public class HelloBean { private String hel ...

  7. Spring读取配置文件的几种方式

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; imp ...

  8. java web路径和spring读取配置文件

    此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...

  9. Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置

    通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值: @PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@ ...

随机推荐

  1. VVDocumenter - Xcod代码注释工具

    刚接触IOS开发时,发现XCODE非常的强大的,后续的代码实践中发现XOCDE的代码文档注释非常的差, 每次都要用手敲,蛋疼至极: 随着不断学习发现XCODE有代码片段内嵌一说(如:for .bloc ...

  2. leetcode -- Search for a Range (TODO)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  3. 哈夫曼(Huffman)编码

    哈夫曼编码(Huffman Coding)是一种非常经典的编码方式,属于可变字长编码(VLC)的一种,通过构造带权路径长度最小的最优二叉树以达到数据压缩的目的.哈弗曼编码实现起来也非常简单,在实际的笔 ...

  4. UNIX标准化及实现之限制

    前言 UNIX系统实现定义了很多幻数和常量,其中有很多已被硬编码(关于硬编码和软编码:http://www.cnblogs.com/chenkai/archive/2009/04/10/1432903 ...

  5. c盘太小

    C:\Users\Administrator\AppData\Roaming\Apple Computer

  6. cooking java ——加密解密

    java安全与密码概述 主要分为三部分: 密码学基础,包括:相关术语:分类:常用安全体系. java的安全组成:jdk以及第三方扩展. 相关实现代码,包括:base64.MD5········ 密码学 ...

  7. python(3)-函数动态参数

    先来看一段代码 def show(*arg): print(arg, type(arg)) if __name__ == "__main__": show(1) show(1,2, ...

  8. jquery手写焦点轮播图-------解决最后一张无缝跳转第一张的问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. HDU 1241 Oil Deposits (DFS/BFS)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  10. HDU 1003 - Max Sum(难度:*)

    Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...