What is a good method of persisting application properties?

前端 未结 8 1961
不思量自难忘°
不思量自难忘° 2021-01-05 09:43

I have a series of application properties which all have different types.

The types could include, booleans, dates, timestamps, or strings.

I need to be abl

相关标签:
8条回答
  • 2021-01-05 10:20

    Message from the future: the link is already dead.

    Java has a facility built specifically for this purpose - Properties. Here is very good article about it

    https://docs.oracle.com/javase/tutorial/essential/environment/properties.html

    0 讨论(0)
  • 2021-01-05 10:20

    Assuming you are using Java, take a look at apache commons DatabaseConfiguration (http://commons.apache.org/configuration/apidocs/org/apache/commons/configuration/DatabaseConfiguration.html).
    Basically, what it does is pretty simple. It scans a table that has key-value pairs and exposes that table as a java.util.Properties. You can use this to load your application properties from the database.
    Once loaded, you can cache these properties in your application. Remember to invalidate this cache whenever you make changes to the application properties.

    0 讨论(0)
  • 2021-01-05 10:29

    java.util.Properties is the easiest way. A Properties object can be created from a properties file (a file containing properties in the format name=value) or even a simple XML file. You can modify the object in memory and then write it back to a properties or XML file.

    If you need more flexibility in structuring the properties, you can consider designing your own XML configuration file, although it will be a bit more work to read and write. You can however use a marshalling/unmarshalling API like JAXB, XStream etc to make that task easier.

    These files can easily be modified manually as well.

    0 讨论(0)
  • 2021-01-05 10:31

    Even though you should use Preferences API or Properties, here is a hack that might also work:

    Create a HashMap<String, Object> and use XMLEncoder/XMLDecoder for saving/loading. Most of the standard java classes can be serialized this way as java beans. You can make a simple wrapper class for getting various properties, handling default values etc. This way you can also store nested collections which is handy sometimes. It's really easy to implement.

    0 讨论(0)
  • 2021-01-05 10:33

    Since you have a requirement of storing and reading various properties of different types like boolean, integers, etc. I think the java.util.prefs API is a good choice for you. It allows you to store and read various data types.

    Here's the API documentation

    0 讨论(0)
  • 2021-01-05 10:34

    I use this code and it works quite good for me (only partial code, coding from memory..): (This is used in conjunction with the Properties class of java, but it makes it easier to add properties and keep your property file template in sync.)

    to use:
    Date date = Conf.value(Prop.SOME_DATE,Date.class);
    

    and

    enum Prop {
        SOME_DATE(Date.class, "2009-10-28", "Some date"){
            Object parse(String value){
                return new Date(value);
            }};
        private final Class<?> type;
        private final String description;
        private final Object default;
        Properties(Class<?> type, String defaultValue, String desc){
           this.type = type;
           this.description = desc;
           this.default = this.parse(defaultValue);
        }
        abstract Object parse(String value);
    }
    

    and

    class Conf {
        private static final String PROP_FILE_NAME = "some.properties";
        private volatile Map<Prop,Object> store;
    
        public void load(){
            //Read from property file and use default if not given.
            //I code it in a way that it will not permit null as value, so 
            //if default is null the user is forced to provide setting.
        }
    
        public <T> T value(Prop prop, Class<T> clazz){
            return (T)this.store.get(prop);
        }
        public static void main(String[] args){
            //code to autogenerate property file
            //Something like:
            //#Default : 2009-10-28 Description : Some date. Type:Date
            //#SOME_DATE=2009-10-28
        }
    }
    

    The class argument makes the method a little verbose, but sometimes it is irritating if you can only let java infer the type, like this:

    CountDownLatch latch = Conf.value(Prop.SOME_INTEGER);
    //Compilation error! Since it expects int and won't unbox.
    
    CountDownLatch latch = Conf.value(Prop.SOME_ITEGER,Integer.class);
    //Verbose, but works everytime. Of course you could get a 
    //ClassCastException, but you should notice that early 
    //in the development...    
    
    0 讨论(0)
提交回复
热议问题