Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load java properties inside static initializer block

Tags:

I have a static util class that does some string manipulation on a bit sensitive data. Prior to use of this class I need to initialize certain static variables with values, such as usernames/password, that I prefer to store in a .properties file.

I am not very familiar with how loading of .properties file work in Java, especially outside of *Spring DI *container. Anyone can give me a hand/insight on how this can be done?

Thank you!

Addition: .properties file precise location is unknown, but it will be on the classpath. Sorta like classpath:/my/folder/name/myproperties.propeties

like image 476
xelurg Avatar asked Jun 25 '09 16:06

xelurg


People also ask

How do you load properties file in Java only once?

Just create class field Properties properties:) And save once loaded values there.

How do you initialize a variable in a static block in Java?

Instance variables are initialized using initialization blocks. However, the static initialization blocks can only initialize the static instance variables. These blocks are only executed once when the class is loaded.

Can you initialize static data member inside Nonstatic block?

Yes... a non-static method can access any static variable without creating an instance of the class because the static variable belongs to the class.


1 Answers

First, obtain an InputStream from which the properties are to be loaded. This can come from a number of locations, including some of the most likely:

  • A FileInputStream, created with a file name that is hard-coded or specified via a system property. The name could be relative (to the current working directory of the Java process) or absolute.
  • A resource file (a file on the classpath), obtained through a call to getResourceAsStream on the Class (relative to the class file) or ClassLoader (relative to the root of the class path). Note that these methods return null if the resource is missing, instead of raising an exception.
  • A URL, which, like a file name, could be hard-coded or specified via a system property.

Then create a new Properties object, and pass the InputStream to its load() method. Be sure to close the stream, regardless of any exceptions.

In a class initializer, checked exceptions like IOException must be handled. An unchecked exception can be thrown, which will prevent the class from being initialized. That, in turn, will usually prevent your application from running at all. In many applications, it might be desirable to use default properties instead, or fallback to another source of configuration, such as prompting a use in an interactive context.

Altogether, it might look something like this:

private static final String NAME = "my.properties";  private static final Properties config;  static {   Properties fallback = new Properties();   fallback.put("key", "default");   config = new Properties(fallback);    URL res = MyClass.getResource(NAME);   if (res == null) throw new UncheckedIOException(new FileNotFoundException(NAME));   URI uri;   try { uri = res.toURI(); }   catch (URISyntaxException ex) { throw new IllegalArgumentException(ex); }    try (InputStream is = Files.newInputStream(Paths.get(uri))) { config.load(is); }    catch (IOException ex) { throw new UncheckedIOException("Failed to load resource", ex); } } 
like image 126
erickson Avatar answered Sep 23 '22 04:09

erickson