Spring 如何读取properties文件内容
http://hi.baidu.com/alizv/blog/item/d8cb2af4094662dbf3d38539.html
在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据我工作中用到的读取properties配置文件的方法小小总结一下,主要叙述的是spring读取配置文件的方法。
用spring读取配置文件,最典型的就是关于数据库的连接,下面就是一个例子:
文件jdbc.properties:
-------------------------------------------------------------------------------------
driverClassName com.MySQL.jdbc.Driver
url jdbc:mysql://localhost:3306/test
username root
password 1234
------------------------------------------------------------------------------------
引入spring的相关jar包,在applicationContext.xml中配置:
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>src/jdbc.properties</value>
</property>
</bean>
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${driverClassName}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${username}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
</bean>
<bean id="dao" class="com.zh.model.DataDAO">
<property name="datasource">
<ref local="datasource"/>
</property>
</bean>
</beans>
-----------------------------------------------------------------------------------------
DataDAO.Java
package com.zh.model;
import javax.sql.DataSource;
public class DataDAO {
private DataSource datasource;
public DataSource getDatasource() {
return datasource;
}
public void setDatasource(DataSource datasource) {
this.datasource = datasource;
}
}
------------------------------------------------------------------------------------
测试连接是否成功,test.java
package com.zh.logic;
import java.sql.Connection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.zh.model.DataDAO;
public class test {
public static void main(String [] args){
try{
String[] path = {"src/applicationContext.xml"};
ApplicationContext ctx = new FileSystemXmlApplicationContext(path);
DataDAO dao = (DataDAO)ctx.getBean("dao");
Connection con = dao.getDatasource().getConnection();
System.out.println(con.isClosed());
//System.out.print(dao.getName());
}catch(Exception ex){
ex.printStackTrace();
}
}
}
-------------------------------------------------------------------------------------
2.用java.util.Properties这个类来读取
比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:
ip=192.168.0.1
port=8080
--------------------------------------------------------------------------------------
则,我们可以用如下程序来获得服务器配置信息:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties");
Properties p = new Properties();
try{
p.load(inputStream);
} catch (IOException e1){
e1.printStackTrace();
}
System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));
--------------------------------------------------------------------------------------
上面介绍了读取properties的内容,现实中我们还有可能要修改文件的内容,下面就看下怎么修改properties的内容,文件还是上面那个:
package com.zh.logic;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;
public class TestRead {
public void read(){
try {
InputStream in = this.getClass().getClassLoader().getResourceAsStream("config/host.properties");
Properties p = new Properties();
p.load(in);
//p.list(System.out);
System.out.println(p.getProperty("ip")+","+p.getProperty("username")+","+p.getProperty("pwd"));
} catch (Exception e) {
e.printStackTrace();
}
}
public void update(String path){
try{
Properties p = new Properties();
FileInputStream in = new FileInputStream(path);
p.load(in);
FileOutputStream out = new FileOutputStream(path);
p.setProperty("ip","1234567");
p.store(out,"ip update");
//p.save(out,"ip updated");
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
TestRead td = new TestRead();
td.read();
td.update("config/host.properties");
td.read();
}
}
可以看见修改之前的和修改之后的内容有改变;在上面有点要注意的:
FileInputStream in = new FileInputStream(path);
p.load(in);
FileOutputStream out = new FileOutputStream(path);
就是p.load(in);要写在FileOutputStream out = new FileOutputStream(path);之前,不然的话,修改后的文件内容就成了ip=1234567,而port=8080这句被覆盖了;什么愿意大家自己想想吧;
上面介绍了两中读取properties文件的方法,希望对大家有帮助........
Spring 如何读取properties文件内容的更多相关文章
- 五种方式让你在java中读取properties文件内容不再是难题
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- 分别用Java和JS读取Properties文件内容
项目中经常用到的配置文件,除了XML文件之外,还会用到Properties文件来存储一些信息,例如国际化的设置.jdbc连接信息的配置等.有时候也会把一些路径或者sql语句放到Properties中, ...
- Spring下读取properties文件
由于在spring的xml文件中配置了 <bean id="validator" class="org.springframework.validation.bea ...
- spring无法读取properties文件数据
只讲述异常点,关于怎么配置文件,这里不做说明. 1. controller中无法读取config.properties文件 controller中注入的@Value配置是从servlet-cont ...
- 在Spring中读取properties文件
1.配置文件(*.properties)往往通过以下方式注册在Spring IOC中. <!-- JDBC配置 --> <context:property-placeholder l ...
- spring使用@Value注解读取.properties文件时出现中文乱码问题的解决
解决办法 在spring中我们常常使用.properties对一些属性进行一个提前配置, spring 在读取*.properties文件时, 默认使用的是asci码, 这时 我们需要对其编码进行转换 ...
- classpath 及读取 properties 文件
java代码中获取项目的静态文件,如获取 properties 文件内容是必不可少的. Spring 下只需要通过 @Value 获取配置文件值 <!-- 资源文件--> <util ...
- 【开发笔记】- Java读取properties文件的五种方式
原文地址:https://www.cnblogs.com/hafiz/p/5876243.html 一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供j ...
- 读取.Properties文件以及Spring注解读取文件内容
public class Main { public static void main(String[] args) throws IOException { //创建Properties对象 Pro ...
随机推荐
- 【转载】python-协程
转载自:廖雪峰的官方网站 协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在所有语言中都是层 ...
- 【转载】python import和from import
import和from import都是将其他模块导入当前模块中. 刚开始一直以为import和from import唯一的区别,就是from import可以少写一些模块名.虽然from XX im ...
- Jmeter组件和属性(二)
Jmeter脚本开发原则 简单.正确.高效.简单:去除无关的组件,同时能复用的尽量复用.正确:对脚本或者业务正确性进行必要的判断,不能少也不能多.(200),业务错误的情况下,也可能返回200,必须用 ...
- Weex 版扫雷游戏开发
扫雷是一个喜闻乐见的小游戏,今天在看 Weex 文档的过程中,无意中发现用 Weex 完全可以开发一个扫雷出来.当然这个扫雷和 Windows 那个有一点差距,不过麻雀虽小五脏俱全,随机布雷.自动挖雷 ...
- 打开Office2007弹出“向程序发送命令时出现问题” 解决方案
打开Office2007弹出“向程序发送命令时出现问题” 解决方案,试了很多方案,最终还是这种方法帮我解决了问题,分享下,以下地址便是: http://club.excelhome.net/threa ...
- CSU 1424 Qz’s Maximum All One Square
原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1424 逐渐找到做这种题的感觉了. 二分法.g[i][j]存储坐标(i, j)的值,s[i ...
- elementUI 学习入门之 container 布局容器
Container 布局容器 用于布局的容器组件,方便快速搭建页面基本结构 <el-container> : 外层容器.当子元素包含 <el-header> 或 <el- ...
- cogs——2098. Asm.Def的病毒
2098. Asm.Def的病毒 ★☆ 输入文件:asm_virus.in 输出文件:asm_virus.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] “这就 ...
- JAVAEE学习——hibernate03:多表操作详解、级联、关系维护和练习:添加联系人
一.一对多|多对一 1.关系表达 表中的表达 实体中的表达 orm元数据中表达 一对多 <!-- 集合,一对多关系,在配置文件中配置 --> <!-- name属性:集合属性名 co ...
- Failed to resolve directive: el vue2报错
vue2报错 Failed to resolve directive: el 为什么会报这个错呢,主要还是因为vue升级的时候,v-el在vue2.x以后被淘汰.使用新的标签ref替换v-el,接下来 ...