I am trying to read properties file using a singleton class. I would like to know if there are any best practices and design patterns to read the properties file. I found dependency injection is one way to read properties file. But, I did not find any examples on the DI design pattern. Any sample code and links would be helpful.
package com.cisco.propertiesreader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
import org.apache.log4j.Logger;
public class ReadProperties {
private static final Logger LOGGER = Logger.getLogger(ReadProperties.class);
private final Properties props = new Properties();
private ReadProperties() {
InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties");
try {
props.load(in);
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
private static class PropHolder {
private static final ReadProperties INSTANCE = new ReadProperties();
}
public static ReadProperties getInstance() {
return PropHolder.INSTANCE;
}
public String getProperty(String key) {
return props.getProperty(key);
}
public Set<String> getAllPropertyNames() {
return props.stringPropertyNames();
}
public boolean containsKey(String key) {
return props.containsKey(key);
}
}
config.properties, butapp1.properties, app2.properties, ... appN.properties? \$\endgroup\$