How to create a dynamic interface with a properties file at compile time?

The problem here is that the properties file we are using has an insanely huge name as a key, and most of us are faced with the wrong key name problems. so I wondered if there was a way to create the following interface based on a properties file. Every change we make in the properties file will automatically configure the Properties interface. Or is there another solution?

Property file

A=Apple
B=Bannana
C=Cherry

Create the following interface

interface Properties{
public static final String A = "A" // keys
public static final String B = "B"; 
public static final String C = "C"; 

}

So in my application code

String a_value = PROP.getString(Properties.A);
+2
source share
2 answers

There is an old rule about programming, and not just about it, if something looks beautiful, then most likely this is the right way.

, .

:

. . : http://en.wikipedia.org/wiki/Constant_interface

:

, - , : key_

, , key_, , .


UPDATE

, , Apache Ant script.

, (myapp.properties) :

key_A = Apple
key_B = Banana
key_C = Cherry
anotherPropertyKey1 = blablabla1
anotherPropertyKey2 = blablabla2

, , , key_.

, ( , , ):

package propertiestest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;


public class PropertiesTest {

  public static void main(String[] args) throws IOException {
       final String PROPERTIES_FILENAME = "myapp.properties";      

       SpecialPropertyKeysStore spkStore = 
                            new SpecialPropertyKeysStore(PROPERTIES_FILENAME);

       System.out.println(Arrays.toString(spkStore.getKeysArray()));

   }
}


class SpecialPropertyKeysStore {

    private final Set<String> keys;

    public SpecialPropertyKeysStore(String propertiesFileName) 
                                    throws FileNotFoundException, IOException {


        // prefix of name of a special property key
        final String KEY_PREFIX = "key_";        

    Properties propertiesHandler = new Properties();
        keys = new HashSet<>();

    try (InputStream input = new FileInputStream(propertiesFileName)) {

            propertiesHandler.load(input);

            Enumeration<?> enumeration = propertiesHandler.propertyNames();
            while (enumeration.hasMoreElements()) {
                String key = (String) enumeration.nextElement();
                if (key.startsWith(KEY_PREFIX)) {
                    keys.add(key);
                }
            }
    }        
    }

    public boolean isKeyPresent(String keyName) {
        return keys.contains(keyName);
    }

    public String[] getKeysArray() {
        String[] strTypeParam = new String[0];

        return keys.toArray(strTypeParam);
    }
}

SpecialPropertyKeysStore .

, .

, :

[key_C, key_B, key_A]

.

, .

+1

, :

  • , java + javadocs
  • , java, . , .

Property . get, Properties, a Map - .

, maven-exec-plugin.

, , :

, , , , , maven .

, POJO, (CDI, Spring, ..).

+1

All Articles