Web application properties file

Firstly, there are many solutions, and I already read a lot. But for some reason I am not working.

I am trying to redirect my configuration data for my webapp so that I can implement it after deployment.

This is my property 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 zero! But I do not know why -.-

Help is needed: -)

here is 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

Now I do the following:

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

And I still get nullPointerException

+3
source share
1 answer

Take it out META-INF, put it in src/configdirectly in the configuration package in the source, after assembly it will go to/WEB-INF/classes/config/config.properties

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

Update enter image description here

and then I created a class Servicein the same project that has a method.

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

It works..!!

+7

Source: https://habr.com/ru/post/1788140/


All Articles