Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?
like image 878
Mrudav Shukla Avatar asked Oct 03 '18 13:10

Mrudav Shukla


1 Answers

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);
like image 172
kj007 Avatar answered Oct 11 '22 19:10

kj007