java - Why not use ResourceBundle instead of Properties? -
this easy question can't find final answer.
i can load string properties (e.g.: query prepared statement) config.properties
file. let's want take database connection connect.
if want take info file, next in class:
private static final resourcebundle bundle = resourcebundle.getbundle("scheduler"); private static final string driver = bundle.getstring("bd.driver"); private static final string connectionurl =bundle.getstring("bd.url"); ....
but instead i've seen many people recommend using instead properties, have same (if want maintain class static , not have proper constructor):
static { prop = new properties(); seek { prop.load(reportsdb.class.getclassloader().getresourceasstream("config.properties")); } grab (ioexception ex) { logger.getlogger(reportsdb.class.getname()).log(level.severe, null, ex); throw new runtimeexception(ex); } } private static final string driver = prop.getproperty("bd.driver"); private static final string connectionurl = prop.getproperty("bd.url");
so, why shouldn’t utilize resourcebundle
instead of properties
when sec 1 more verbose?
so, why shouldn’t utilize resourcebundle instead of properties when sec 1 more verbose?
because that's not resourcebundle
for. description of class starts with:
resource bundles contain locale-specific objects. when programme needs locale-specific resource, string example, programme can load resource bundle appropriate current user's locale. in way, can write programme code largely independent of user's locale isolating most, if not all, of locale-specific info in resource bundles.
does of sound utilize case? don't think so.
it sounds problem purely verbosity of loading properties file: write utility method that. code can simply:
private static final properties configuration = propertyutil.load("scheduler.properties"); private static final string driver = configuration.getstring("bd.driver"); private static final string connectionurl = configuration.getstring("bd.url");
admittedly i'm not keen on having static field initializers in order-dependent way that... i'd tempted encapsulate of configuration in separate class, write:
private static final schedulerconfiguration config = schedulerconfiguration.load("scheduler.properties");
then utilize config.getdriver()
etc fetch properties each time, or utilize field, or whatever.
java
No comments:
Post a Comment