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.
Péter Török
source share