How to use properties file from a nested folder inside /src/main/resources?

Mrudav Shukla :

I have a spring boot application where I want to externalize the messages. I am categorizing these messages into errors, info, and success. Thus I'm creating a nested folder structure as below:

enter image description here

And, I'm trying to access this from the service in the following manner:

Locale locale = new Locale("en");
ResourceBundle errors = ResourceBundle.getBundle("errors", locale); 
System.out.println(errors.getString("E000"));

This gives me the following exception:

java.util.MissingResourceException: Can't find bundle for base name errors, locale en

However, if I keep the properties file outside the folders as below, it works fine:

enter image description here

So,

  1. How do I make sure that the ResourceBundle is able to find the property file located inside the nested folder?
  2. Can it be done without updating the classpath?
  3. Is this an apt way to manage messages of the application? Or are there still better ways to externalize and manage these error, info and success messages?
kj007 :

Use below will work: as resources is your class path so you need to pass messages also..if you will move out errors file from messages to classpath so it works as by default classpath is resources..

ResourceBundle errors = ResourceBundle.getBundle("messages/errors/errors", locale);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=80876&siteId=1