Reputation: 5578
in java, I am reading from a property file into a Properties object:
static Properties fileProp = null; // holds file properties
public static void main( final String[] args ) throws IOException
{
...
//load a properties file
InputStream is = LearnButtonPressHttpHandler.class.getResourceAsStream("/rsrc/file.properties");
fileProp.load(is);
...
// more objects are created
}
later, deeper into the code, several layers deeper, I need to access this properties object but find that I don't have access to this fileProp object. I don't want to pass this object as a parameter down the many layers to where I need it. It would solve the problem but does not seem to be such an elegant solution. Is there a better way?
Upvotes: 3
Views: 98
Reputation: 3679
Create Singleton class which performs the property loading( I prefer Singleton rather than static class). Then you can access properties from any where.
public class Singleton {
private static Singleton INSTANCE = null;
Properties props = new Properties();
public static final Singleton getInstance() throws IOException {
if (INSTANCE == null) {
synchronized (Singleton.class) {
if (INSTANCE == null) {
INSTANCE = new Singleton();
INSTANCE.loadProperties();
}
}
}
return INSTANCE;
}
private void loadProperties() throws IOException{
InputStream is = Singleton.class.getResourceAsStream("/rsrc/file.properties");
props.load(is);
}
public Object getData(String key){
return props.get(key);
}
}
Upvotes: 1
Reputation: 31238
The simplest way in plain Java is to use the Singleton
pattern, load your properties singleton at application startup or have a getProperties
method in that class that returns the properties if they have been loaded and load them if not.
For example:
public class MySingleton {
private static MySingleton instance = null;
protected MySingleton() {
// Exists only to defeat instantiation.
}
public static MySingleton getInstance() {
if(instance == null) {
instance = new MySingleton();
instance.loadData()
}
return instance;
}
private void loadData(){
//doSomething
}
}
In this case, you can call MySingleton.getInstance()
, and that will get you the data object that you want and not reload it if it was previously loaded. If it is the first time, it will load it lazily.
If you are using a framework or an application server, there is a multitude of ways depending on what your stack offers. E.g. in Spring
, singleton is the default bean type.
Upvotes: 1
Reputation: 11733
This is one of the major things dependency injection does. For instance, in Spring there is a PropertyPlaceholderConfigurer that is used to inject properties from one place into beans as they are being constructed (cf. the Prototype Pattern in Gang of Four).
Many frameworks just do static property management, e.g. Play. Then you are basically implementing a Singleton.
Upvotes: 0
Reputation: 114320
Create a static
getter for the fileProp
reference:
public static Properties getFileProp()
{
return fileProp;
}
This way, any other piece of code that needs it, can access it.
Upvotes: 1