简体   繁体   中英

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.

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

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

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

Update 在此输入图像描述

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");
}

This works..!!

Compare yours

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM