简体   繁体   English

Web应用程序中的属性文件

[英]properties file in web app

First, there are a lot of solutions out there and I have already read a lot of them. 首先,有很多解决方案,我已经阅读了很多解决方案。 But for some reason I don't get it working. 但由于某种原因,我没有让它发挥作用。

I am trying to outsource my config data for my webapp, so that I can cnange it after deployment. 我正在尝试将我的配置数据外包给我的webapp,以便我可以在部署之后进行配置。

That is my properties service: 那是我的物业服务:

   public class PropertiesService {

 Properties properties;
     public PropertiesService() {
      try {
       properties = new Properties();
       ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
       InputStream stream = classLoader.getResourceAsStream("META-INF/config.properties");
       properties.load(stream);
      } catch (Exception e) {
       e.printStackTrace();
      }
     }

     public String getHost(){
      return properties.getProperty("server_host");
     }

     public String getServerName(){
      return properties.getProperty("server_naming");
     }
    }

After debugging I noticed that the variable stream remains null! 调试后我注意到变量流仍为空! But I don't know why -.- 但我不知道为什么 - .-

Need help :-) 需要帮忙 :-)

here the error log: 这里的错误日志:

java.lang.NullPointerException
 at java.util.Properties$LineReader.readLine(Properties.java:418)
 at java.util.Properties.load0(Properties.java:337)
 at java.util.Properties.load(Properties.java:325)

Update 更新

I do the following now: 我现在做以下事情:

properties.load(this.getClass().getResourceStream("/config/config.properties"));

And I still get a nullPointerException 我仍然得到一个nullPointerException

Take out from META-INF , put it in src/config direct to config package in source , upon build it will go to /WEB-INF/classes/config/config.properties 取出META-INF ,将它放在src/config直接配置源代码中的包,在构建时它将转到/WEB-INF/classes/config/config.properties

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

Update 更新 在此输入图像描述

and then I created a Service Class in the same project which has a method. 然后我在同一项目中创建了一个具有方法的Service类。

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

This works..!! 这工作.. !!

Compare yours 比较你的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Web 应用程序和属性文件 - Java web app and properties file 如何在运行时在Web应用程序中修改属性文件? - How to modify properties file in a web app at runtime? Spring Web应用程序:如何外部化属性文件? - Web app with Spring: how externalize properties file? 无法在Java Web应用程序中读取属性文件? - Cannot read properties file in Java web app? Web Java应用程序中的xml属性文件位置 - xml properties file location in web java app 将属性文件放在Struts 1.3的Web应用程序文件夹中的哪里? - Where to put the properties file in web app folder in Struts 1.3? 在 Java Jersey RESTful Web 应用程序中加载属性文件,以在整个应用程序中持续存在? - Loading properties file in a Java Jersey RESTful web app, to persist throughout the app? 更新Web应用程序中的属性文件 - updating a properties file in a web application 如何在 spring web 应用程序中找到 application.properties,虽然位置被提及为 classpath:/application.properties 但文件丢失? - How to find the application.properties in spring web app, while location is mentioned as classpath:/application.properties but file is missing? 在部署到tomcat服务器的Spring Web应用程序中,catalina.properties文件由spring自动加载和检测。 怎么样? - In Spring web app deployed to tomcat server, catalina.properties file is automatically loaded and detected by spring. HOW?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM