spring读取prperties配置文件(1)
博客地址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配置文件(1)的更多相关文章
- spring读取prperties配置文件(2)
接上篇,spring读取prperties配置文件(1),这一篇主要讲述spring如何用annotation的方式去读取自定义的配置文件. 这里我先定义好属性文件"user.propert ...
- Spring读取xml配置文件的原理与实现
本篇博文的目录: 一:前言 二:spring的配置文件 三:依赖的第三方库.使用技术.代码布局 四:Document实现 五:获取Element的实现 六:解析Element元素 七:Bean创造器 ...
- spring读取xml配置文件(二)
一.当spring解析完配置文件名的占位符后,就开始refresh容器 @Override public void refresh() throws BeansException, IllegalSt ...
- spring 读取yaml配置文件
从Spring框架4.1.0增加了对YAML的支持,Spring框架4.1.0 maven POM具有Snakeyaml依赖性 . 您可以在Spring Boot应用中使用两种方式加载YAML: 1 ...
- Spring 读取XML配置文件的两种方式
import org.springframework.context.ApplicationContext; import org.springframework.context.support.Cl ...
- Java中spring读取配置文件的几种方法
Spring读取配置XML文件分三步: 一.新建一个Java Bean: package springdemo; public class HelloBean { private String hel ...
- Spring读取配置文件的几种方式
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; imp ...
- java web路径和spring读取配置文件
此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...
- Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置
通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值: @PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@ ...
随机推荐
- BZOJ 2705: [SDOI2012]Longge的问题 GCD
2705: [SDOI2012]Longge的问题 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnl ...
- 用C#调用Matlab图像处理自制QQ游戏2D桌球瞄准器
平时不怎么玩游戏,有时消遣就玩玩QQ里的2D桌球,但是玩的次数少,不能像骨灰级玩家一样百发百中,肿么办呢?于是某天突发奇想,决定自己也来做个“外挂”.说是外挂,其实只是一个瞄准器,毕竟外挂是修改别人的 ...
- Android_通过传感器抓小偷
package com.beyond.phonestolen; import android.hardware.Sensor; import android.hardware.SensorEvent; ...
- HDU 1999 不可摸数
/* 中文题意: 中文翻译: 题目大意:见红字(例如以下) 解题思路:打表,将每一个数的合数之和存在一个数组之中 难点具体解释:用两个for循环写的,第二个for循环主要是解释两个数相乘不超过这个最大 ...
- [Redux + Webpack] Hot reloading Redux Reducers with Webpack
Webpack will hot reload the component, but the reducer we need hard refresh. To sovle the problem, g ...
- 使用命令xrandr设置当前系统的显示分辨率及显示的旋转脚本
/********************************************************************* * Author : Samson * Date ...
- 通过缓存数据库结果提高PHP性能(转)
众所周知,缓存数据库查询的结果可以显著缩短脚本执行时间,并最大限度地减少数据库服务器上的负载.如果要处理的数据基本上是静态的,则该技术将非常有效.这是因为对远程数据库的许多数据请求最终可以从本地缓存得 ...
- PAT 1004
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 支持https请求以及https请求的抓包
iOS9推出的时候,苹果希望大家使用https协议,来提高数据传输之间的安全性.下面我就从最简单的代码介绍,如何在工程中设置,来支持https的请求. 一.证书准备篇 1.证书转换 在服务器人员,给你 ...
- C# 之 Stream 和 byte[] 的相关转换
1.二进制转换为图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(m ...