I have started reading the Apache commons documentation but its very extensive so I am hoping someone can answer a simple question so I don't need to read all of it just to start using it for basic configuration. I am losing patience really quick with it - there is no "quick start chapter" and I don't need to know every detail before I decide if I want to use the library or not.
I want (what I think is a common use-case) a class with static methods that provides look up of properties.
E.g. in class Foo i can use
Settings.config.getString("paramter");
Where
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.DefaultConfigurationBuilder;
/**
* Settings configuration class
*/
public class Settings {
private static final DefaultConfigurationBuilder factory = new DefaultConfigurationBuilder("config.xml");
public static final Configuration config= factory.getConfiguration();
}
The problem is that the factory method can throw an exception! So this code does not compile, a class cannot throw an exception either so I suspect that I need to do a lot more coding.
I suspect that there is a simple solution to this. But it surely cannot be calling
DefaultConfigurationBuilder factory = new DefaultConfigurationBuilder("config.xml");
Configuration config= factory.getConfiguration();
In every class where I want to read configurations?
I have tried:
public class Settings {
public static final Configuration config;
static {
try {
DefaultConfigurationBuilder factory;
factory = new DefaultConfigurationBuilder("config.xml") ;
config = factory.getConfiguration();
}
catch (ConfigurationException e) {
// Deal with the exception
config=null;
System.exit(1);
}
}
}
But I get the compilation error:
error: variable config might already have been assigned
[javac] config=null;