简体   繁体   中英

How do load a properties file and pass it to a java singleton (enum)

I have a java singleton class that has my application settings.

I used this approach: What is an efficient way to implement a singleton pattern in Java?

So I have:

public enum MySettings {

   INSTANCE;

   // bunch of private vars


   private MySettings {
     // load a json file and set my properties
     JsonParser parser = jf.createJsonParser(new File("HARD_CODED_PATH_HERE"));
   }

   // public getters/setters here


}

So the problem is the hard coded path I have currently.

I created a settings.properties file in my /WEB-INF/ folder, now the problem is the only way I know how to load a properties file, it requires the servlet context:

Properties prop = new Properties();
propertiesload(getServletContext().....);

Is there another way to load this?

So is this properties file a static representation of the properties file? ie it is very fast and effecient?

can you try putting your properties directly under src folder rather than WEB-INF folder?

WORKDIR
+ SRC
| + PACKAGES
| | + org.personal.foo  
| - system.properties
| + WEB-INF

Spring has facilities for exposing properties files as beans. You might wanna go that route, given that you tagged your post with "Spring".

Here's one guy's take on how to load properties files using Spring:

http://www.zparacha.com/how-to-read-properties-file-in-spring/#.T2JQqt5SRfQ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM