如何从我的Java Web服务访问.properties文件 [英] How to access a .properties file from my Java Web Service

查看:85
本文介绍了如何从我的Java Web服务访问.properties文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用tomcat成功部署了Java Web服务.该Web服务正在访问配置文件(.Properties),我已将config.properties文件放置在以下目录中

C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ProcurementWS1\WEB-INF\classes\MainPackagePr

ProcurmentWS1是WAR文件的名称

这是尝试从我的代码访问它的方式:

Properties properties = new Properties();
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("procwsconfig.properties");
            properties.load(is);

并且在加载时出现空指针异常

你能告诉我哪里做错了吗?

解决方案

将其放在src/config中直接放在源中的config包中,构建后将转到/WEB-INF/classes/config/config.properties

this.getClass().getResourceAsStream("/config/config.properties");

然后我在具有方法的同一项目中创建了一个服务类.

public static InputStream getConfigAsInputStream(){
    return Service.class.getResourceAsStream("/config/config.properties");
}

I have deployed my java web service successfully using tomcat. This web service is accessing a configuration file (.Properties) I have placed the config.properties files in the following directory

C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ProcurementWS1\WEB-INF\classes\MainPackagePr

ProcurmentWS1 is the name of the WAR file

this is how am trying to access it from my code:

Properties properties = new Properties();
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("procwsconfig.properties");
            properties.load(is);

And am getting a null pointer exception on load

can you tell me what am doing wrong?

解决方案

put it in src/config direct to config package in source , upon build it will go to /WEB-INF/classes/config/config.properties

and this.getClass().getResourceAsStream("/config/config.properties");

and then I created a Service Class in the same project which has a method.

public static InputStream getConfigAsInputStream(){
    return Service.class.getResourceAsStream("/config/config.properties");
}

这篇关于如何从我的Java Web服务访问.properties文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆