Skip to content

Instantly share code, notes, and snippets.

@nilandev
Created November 17, 2015 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nilandev/6646a87edf1052aebf75 to your computer and use it in GitHub Desktop.
Save nilandev/6646a87edf1052aebf75 to your computer and use it in GitHub Desktop.
Spring MVC messageSource configuration via annotation
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
@Configuration
public class MessageConfig {
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:message.properties", "classpath:ValidationMessages.properties");
// if true, the key of the message will be displayed if the key is not
// found, instead of throwing a NoSuchMessageException
messageSource.setUseCodeAsDefaultMessage(true);
messageSource.setDefaultEncoding("UTF-8");
// # -1 : never reload, 0 always reload
messageSource.setCacheSeconds(0);
return messageSource;
}
}
Copy link

ghost commented May 4, 2017

Great Job Dude!

@taher435
Copy link

I don't think you need to add classpath: in setBasenames as Resource Bundle by default looks into the classpath. For me, adding classpath was not working. After removing it, the above code worked for me.

@gasperlf
Copy link

how I can use when I want get a specific value? could you please give me one example? thanks

@kayamuhendiss
Copy link

kayamuhendiss commented Feb 27, 2019

Great job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment