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
Just create class field Properties properties:) And save once loaded values there.
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.
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.
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:
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.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.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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With