开发者

properties file in web app

First, there are a lot of sol开发者_如何学Goutions 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

properties file in web app

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜