1

I have a scenario, I am trying to implement error code mechanism in my application.this is the approach what I am following currently.

    Properties prop = new Properties();
            InputStream input = null;

            try {

                String filename = "ErrorCode.properties";
                input = getClass().getClassLoader().getResourceAsStream(filename);
                if (input == null) {
                    log.debug("Sorry, unable to find " + filename);
                    return null;
                }

                prop.load(input);

                Enumeration<?> e = prop.propertyNames();
                while (e.hasMoreElements()) {
                    String key = (String) e.nextElement();
                    String value = prop.getProperty(key);
                    log.debug("Key : " + key + ", Value : " + value);

                }

But I need error code in lots of different classes and I don't want to write the above code all the time in different classes.

Is there any other way it initialize the property file once and use it any where in the class.

How do I achieve this?

What are different possible ways to achieve this?

I am using spring, is there any way to achieve this in spring?

I am open for any other mechanism instead of property file.

Varun
  • 4,342
  • 19
  • 84
  • 119
  • If it's error codes, you might want to localize them - in which case check here for a solution: http://stackoverflow.com/questions/6246381/getting-localized-message-from-resourcebundle-via-annotations-in-spring-framewor – Jan May 03 '16 at 07:59

2 Answers2

0

Why don't you just implement a separate singleton class, something like "ErrorCodes", initialize your property file once during object creation and then retrieve the file via a getter?

0

Define a Constants.java and add the properties that you want to use throughout your application runtime. These properties can be used by other classes and dont need to be initialised for every class definition.

public class Constants {
public static String[] tableAndColumnNames;
public static String[] getTableAndColumnNames() {
        return tableAndColumnNames;
    }
    public static void setTableAndColumnNames(String tableNames) {
        Constants.tableAndColumnNames = tableNames.split(";");
    }

public static String getDB() {
        return DB;
    }
public static final String HSQLBD = "HSQLDB";
    public static final String ORACLE = "ORACLE";
public static String DB;
    public static void setDB(int dB) {
        if (dB == 0) {
            Constants.DB = HSQLBD;
        } else {
            Constants.DB = ORACLE;
        }
    }
}

Load these properties at application launch. When you are loading the properties just do a set constants like below

public static void loadProperties() {
Properties property = new Properties();
            InputStream input = CommonUtilities.class.getResourceAsStream("/DB.properties");
            property.load(input);
            Constants.setDB(Integer.parseInt(prop.getProperty("DB")));
Constants.setTableAndColumnNames(property.getProperty("tableAndColumnNames"));
}

And after the properties have been initializedd you can use it at any point of program just by referring the Constant class. Like

public void someOtherClass()

public static void main(String[] args) {
    if(this.DB.equals(Constans.getDB()) // will return the DB from the properties class
    // no need to initialize properties again in every class.
}

Hope this helps.

mattyman
  • 351
  • 2
  • 16