5
\$\begingroup\$

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);
    }
}
\$\endgroup\$
  • \$\begingroup\$ This seems to be an exact duplicate of your earlier question... \$\endgroup\$ – h.j.k. Mar 15 '16 at 1:14
  • \$\begingroup\$ What if the file is not named config.properties, but app1.properties, app2.properties, ... appN.properties? \$\endgroup\$ – h.j.k. Mar 15 '16 at 1:17
  • \$\begingroup\$ The question is not duplicate.Here,I am emphasizing more on the dp needs to be used. \$\endgroup\$ – wandermonk Mar 15 '16 at 1:34
  • \$\begingroup\$ If the file names don't change you could use an enum to represent the singleton pattern or even a pool if you have more than one. \$\endgroup\$ – MAG Mar 15 '16 at 2:24
  • \$\begingroup\$ hi @MAG could you please help me with an example of creating a pool \$\endgroup\$ – wandermonk Mar 15 '16 at 4:28
1
\$\begingroup\$

DI means injecting dependencies in and not creating them directly in a class. Your properties file contains the String value of the keys. You need not compile your java classes if the string value are changed. Thus your string literals and your classes are de-coupled from each other.

Dependency Injection is a Design principle. (I suggest you to study SOLID design Principles). Along with it also we should pay due respects to principles like KISS and YAGNI. These say we should keep the code simple and avoid over-engineering.

You should have different Properties object for each file and pass the object from which you wish to read in your PropHolder.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.