读取web外的配置文件
一般web项目配置文件都放在classPath下面,读取的时候:
1 import java.io.InputStream;
2 import java.util.Properties;
3 public class DealConnection {
4 private static String url = "";
5 private static String user = "";
6 private static String pwd = "";
7 private static String connStr="";
8 //构造方法
9 public DealConnection() {
10 InputStream istr = DealConnection.class.getClassLoader().getResourceAsStream("/parse.properties");//获取classPath下面的配置文件
11 Properties p=new Properties();
12 p.load(istr);
13 istr.close();
14 //获取各个配置的值
15 url=p.getProperty("url").trim();
16 user=p.getProperty("user").trim();
17 pwd=p.getProperty("pwd").trim();
18 connStr=p.getProperty("connStr");
19 }
20 //数据库连接方法
21 public Connection openConn(){
22 Connection conn=null;
23 try {
24 Class.forName(connStr).newInstance();
25 conn = DriverManager.getConnection(url, user, pwd);
26 } catch (Exception e) {
27 e.printStackTrace();
28 }
29 return conn;
30 }
31 }
现在如果想把配置文件放到外面,让别人也可以方便修改配置文件,假设:配置文件放到tomcat/bin/conf文件下,就修改读取文件那里
public DealConnection() {
String name=System.getProperty("user.dir");//得到的路径在tomcat/bin路径
InputStream istr;
try {
istr = new FileInputStream(name + "/conf/parse.properties");//读取就是tomcat/bin/conf下的配置文件
Properties p=new Properties();
p.load(istr);
istr.close();
url=p.getProperty("url").trim();
user=p.getProperty("user").trim();
pwd=p.getProperty("pwd").trim();
connStr=p.getProperty("connStr");
} catch (Exception e) {
e.printStackTrace();
}
applicationContext.xml,在读取classPath配置文件的时候,路径classpath:jdbc.properties
<!-- 属性文件的位置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
如果要读取tomcat/bin/conf下的配置文件,路径改成:file:conf/jdbc.properties,它是相对于web容器的
<!-- 属性文件的位置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:conf/jdbc.properties</value>
</property>
</bean>
读取web外的配置文件的更多相关文章
- jar包中的类如何读取包内和包外的配置文件
最近将代码打包成jar包,关于如何处理读取配置文件的问题特此记录一下. out.properties a.jar -com -a.class -in.properties 如上所示,out.prope ...
- C#Unit单元测试之读取Web.config文件
长期一来,我们所完成的项目都没有写单元测试,今天我一时兴起,决定给自己写的代码写单元测试,简单的测试代码分分钟完成了,一运行测试,就懵逼了.没能达到我的预期效果,而是出现图1所示错误. 图1:单元测试 ...
- 读取本地外网IP地址
读取本地外网IP地址. 根据启动并运行的网卡名称,找到本机实际的IP地址(已知当前运行的无线网卡名包含某一个字符) import java.net.InterfaceAddress; import j ...
- C#之读取web上的xml
一.使用LINQ读取使用Xdocument上的Load方法,可以快速的加载一个XML文档,然后使用LINQ对 加载XML文档进行查询或其他操作,这里仅简单偏历.所以,一旦查询一组元素有返回元素集,就可 ...
- SpringBoot学习(三)-->Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池
三.读取外部的资源配置文件并配置数据库连接池 1.读取外部的资源配置文件 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: @Configuration ...
- Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置
通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值: @PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@ ...
- Setup Factory 读取安装包的配置文件
result = INIFile.GetValue(SessionVar.Expand("%SourceFolder%\\set.ini"), "set", & ...
- 仿联想商城laravel实战---6、自建配置文件和缓存(如何读取自己创建的配置文件的信息)
仿联想商城laravel实战---6.自建配置文件和缓存(如何读取自己创建的配置文件的信息) 一.总结 一句话总结: config()及相应的方法 1.前端插件选择好了,后端开发超级省力? 比如多图上 ...
- 读取web应用下的资源文件(例如properties)
package gz.itcast.b_resource; import java.io.IOException; import java.io.InputStream; import java.ut ...
随机推荐
- 定位CPU高问题三把斧
1.top -H -p PID 查看对应进程的哪个线程占用CPU过高 2.printf "%x\n" tid 将需要的线程ID转换为16进制格式 3.jstack pid &g ...
- jmeter测试FTP
1.下载并运行FTP服务器软件:Quick Easy FTP Server V4.0.0.exe 2.点击右上角的绿色按钮,开启服务器,直到中间的红色按钮亮起 3.在账户管理处可以管理账号信息(用户名 ...
- Python中for、while、break、continue、if的使用
1.if - elif - else 的使用 格式:if 条件1: 条件1满足时执行的事件1 条件2满足时执行的事件2 elif 条件2: 条件2满足执行事件3 条件2满足执行事件4 e ...
- Mac终端建立替身 并置于桌面或Finder中
前情 Xcode存放log的文件夹路径忒长了,且需要用终端才能查看.所以就想制作个替身,放在Finder中便于查看. going on command+space打开terminal 一直cd...进 ...
- js之隔行换色
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- with异常
with 语句 语法: with 表达式1 [as 变量1], 表达式2 [as 变量2], ...: 语句块 作用: 使用于对资源进行访问的场合,确保使用过程中不管是否发生异常都会执行必要的'清理操 ...
- Eclipse远程调试Java程序
1. 在服务器上运行jar包时加入参数 -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address= 2. 在Eclipse中操作 ...
- Chrome 插件下载
这里推荐几个下载chrome扩展的网站 http://www.cnplugins.com/index.html 分类全,没有搜索 http://www.chromein.com/ 有搜索,推荐使用 h ...
- 转载maven安装,配置,入门
转载:http://www.cnblogs.com/dcba1112/archive/2011/05/01/2033805.html 本书代码下载 大家可以从我的网站下载本书的代码:http://ww ...
- SpringMVC和Freemarker整合,带自定义标签的使用方法
SpringMVC和Freemarker整合,带自定义标签的使用方法. [参考来源:http://www.360doc.com/content/14/1225/14/1007797_435663342 ...