Make Java properties available for all classes?

I decided to take the properties file to configure some parameters. I use the following code to make a property object available in a class

Properties defaultProps = new Properties(); try { FileInputStream in = new FileInputStream("custom.properties"); defaultProps.load(in); in.close(); } catch (Exception e) { e.printStackTrace(); } 

Does this need to be added to each class? Probably not because then each class will open a stream to this file. But I'm not sure how to handle this. Should I create a MyProperties class and instantiate in any class requiring properties?

Thanks in advance!

+11
source share
8 answers

After initializing defaultProps you can make its contents available to other objects in your application, for example. through a public static access method, for example:

 public class Config { private static Properties defaultProps = new Properties(); static { try { FileInputStream in = new FileInputStream("custom.properties"); defaultProps.load(in); in.close(); } catch (Exception e) { e.printStackTrace(); } } public static String getProperty(String key) { return defaultProps.getProperty(key); } } 

This is the easiest approach, however it creates an additional dependency that complicates unit testing (unless you provided a method in Config to set the mock properties object for unit testing).

An alternative is to enter defaultProps (or individual configuration values ​​from it) into each object that needs it. However, this may mean that you need to add additional parameters to a variety of methods if call hierarchies are deep.

+12
source

If you need only one instance of the property class, you can use singleton (anti?) -Pattern .

This will look like a class:

 public class MyProperties extends Properties { private static MyProperties instance = null; private MyProperties() { } public static MyProperties getInstance() { if (instance == null) { try { instance = new MyProperties(); FileInputStream in = new FileInputStream("custom.properties"); instance.load(in); in.close(); } catch (Exception e) { e.printStackTrace(); return null; } } return instance; } } 
+4
source

Why not use a static ResourceBundle?

 static final ResourceBundle myResources = ResourceBundle.getBundle("MyResources", currentLocale); 
+1
source

Too little information to determine what would be the best way to handle this. You can view it with accessories or transfer it to each class that requires it. In addition, you can pull out the properties needed by each class and pass their values ​​to the class constructor.

0
source

Load the properties once, using and saving the properties as other classes can retrieve. If this is the MyProperties class, which somewhere refers to a static variable.

0
source

This is a special occasion to make something available around the world. Using static methods is pretty bad. A better but bad solution uses the sigleton pattern. Testing is the biggest problem here. IMHO, the best way is to use dependency injection , although this may be excessive for small applications.

0
source

Since this information is static in all instances, I recommend implementing the Properties class as singleton . Using the static initialization block , you can load the file automatically when the program starts.

 public class Properties { static { try { FileInputStream in = new FileInputStream("custom.properties"); load(in); in.close(); } catch (Exception e) { e.printStackTrace(); } } protected static void load(FileInputStream in) { // existing load functionality here } } 

You will still need the internal memory mechanism and access mechanism. They should also be marked static .

0
source

Instead of loading properties in each class. Download it somewhere around main () and pass it to other classes through their constructors.

Do not share them all over the world. - It’s hard to verify - Against abstraction (global access, DAO can access user settings, it should be prevented by passing only what it needs ... not all) - Classes lie what they need

0
source

All Articles