博客地址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. Codeforces Round #331 (Div. 2) D. Wilbur and Trees 记忆化搜索

    D. Wilbur and Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/p ...

  2. HDU 4588 Count The Carries 数学

    Count The CarriesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  3. Spring中ClassPathXmlApplicationContext类的简单使用

    转自:http://www.cnblogs.com/shyy/archive/2011/09/29/2453057.html 一.简单的用ApplicationContext做测试的话,获得Sprin ...

  4. 语法:MySQL中INSERT INTO SELECT的使用(转)

    1. 语法介绍      有三张表a.b.c,现在需要从表b和表c中分别查几个字段的值插入到表a中对应的字段.对于这种情况,可以使用如下的语句来实现: INSERT INTO db1_name (fi ...

  5. oc-02-NSLog使用

    #import <Foundation/Foundation.h> /* NSLog() : C语言中的 printf的增强版 作用: 向控制台输出打印数据. 增强的地方 : 打印时间 , ...

  6. How to Display Image In Picturebox in VC++ from Iplimage and Mat

    Introduction This tip/trick will be useful to OpenCV programmers, who wish to use Windows Form appli ...

  7. System.Data.SQLite.EF6

    2015.1.21 到目前为止这个破玩意不支持code first 建数据库 建表 代替方案   SQL Server Compact -------------------------------- ...

  8. mysql导入数据库

     mysql -u root -p bbs < d:\bbs_2011-06-15 --default-character-set=gbk      mysqldump -uroot -p ta ...

  9. jQuery Validate 插件[表单验证]

    在客户端添加信息提交表单时我们经常需要做一些验证,比如验证不能为空,验证客户输入手机格式,验证客户输入email,url等的格式,我们可以通过EL表达式结合js 进行自主验证,今天总结一个JQuery ...

  10. [Java,MVC] SpringMVC+Spring+hibernate 框架

    转自:http://my.oschina.net/Thinkeryjgfn/blog/158951 1.准备的jar包以及配置文件如下: 2.新建一个JAVA web项目 3.建好以后出现以上包结构即 ...