Load java properties inside static initializer block -


I have a stable access class that manipulate some strings on slightly sensitive data before using this class, With some stable variables, such as the need to start a username / password, I prefer to store in the .properties file.

I'm not in Java, it is very familiar, how is the work of file .properties being loaded exclusively outside the * Spring D * container.

In addition: .properties

The exact location of the file is unknown, but it will be on the verandah. Sort like square path: /my/folder/name/myproperties.propeties

First of all, get a InputStream , from which the assets have to be loaded. It can come from many places, some of which are most likely to be:

  • A, is created with a file name that is hard-coded or specified by name
  • A resource file (a file on a classpath), via the call received getResourceAsStream (relative to the class file) or (relative to the root of the square path).
  • A, which can be specified by a hard-coded or system property, just like a filename.

Then create a new property object, and enter InputStream in its method. Be sure to turn the stream off, regardless of any exceptions.

In the beginning of the class, exceptions like IOException should be handled, an uncontrolled exception can be thrown, which will prevent the class from starting. This, in turn, will usually prevent your application from running at all. In many applications, instead of using the default property may be desirable, or a fallback to another source of configuration, such as prompting use in an interactive context.

Overall, something like this might appear:

Private Static Last String NAME = "my.properties"; Personal fixed last attribute config; Static {properties fallback = new property (); Fallback.put ("key", "default"); Config = new property (fallback); URL res = MyClass.getResource (NAME); If (res == faucet) new unchecked AIOXation throw (new filenoutfound exception (NAME)); URI Yuri; Try {uri = res.toURI (); } Catch (URIsXx Expectations) {new illegal litigation exception (ex); } Try (InputStream = Files.newInputStream (paths.get (uri))) {config.load (is); } Hold (IOException pre) {new uncheckiation ("failed to load resource", prior); }}

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -