Search code examples
javaconfigresourcebundle

How do I use ResourceBundle to avoid hardcoded config paths in Java apps?


I'd like to eliminate dependencies on hardcoded paths for configuration data in my Java apps, I understand that using ResourceBundle will help me use the classloader to find resources.

Can someone tell me how I would replace a hardcoded path to a resource (say a .properties configuration data file required by a class) with appropriate use of ResourceBundle? Simple clear example if possible, thanks all.


Solution

  • Prior to Java 6, ResourceBundle typically allowed:

    Java 6 comes with the ResourceBundle.Control class which opens the door to other sources of ResourceBundles, for example:

    • XML files (see example 2 in Javadoc)
    • Database rows

    Hope this helps.