简体   繁体   中英

MessageSource doesn't reload properties file

There is a messageSource Bean:

@Bean
public MessageSource messageSource(){
    ReloadableResourceBundleMessageSource messageSource=new ReloadableResourceBundleMessageSource();
    messageSource.setDefaultEncoding("UTF-8");
    messageSource.setBasenames("classpath:/messages/messages");
    return messageSource;
}

I have read somewhere that If I use ReloadableResourceBundleMessageSource messageSource shouldn't be cached and everytime look into the properties file if theere is particular string. Sadly with bean created that way It doesn't work. After compiling If I add some properties Spring Boot won't find it. Properties file is in /resources/messages/messages.properties and /resources /messages/messages_en.properties .

Try this solution. First: configure the bean in your web configuration as shown below.

@Bean
public MessageSource messageSource () {
    ReloadableResourceBundleMessageSourceExt messageResource =
            new ReloadableResourceBundleMessageSourceExt();
    messageResource.setAlwaysUseMessageFormat(false);
    messageResource.setBasenames("classpath:messages");
    messageResource.setDefaultEncoding(CHARACTER_ENCODING);
    messageResource.setFallbackToSystemLocale(true);
    messageResource.setUseCodeAsDefaultMessage(false);
    messageResource.setCacheSeconds(1); // by default it set to -1 which means cache 
                                        // forever messageSourse.
                                        // set 0 to check reload messeageSource on 
                                        // every getMessageSource request but reload 
                                        // only those files which last modified 
                                        // timestamp is changed.
                                        // value greater than 1 is treated as the 
                                        // time interval between reload.
    return messageResource;
}

Second: create a class which extends ReloadableResourceBundleMessageSource to expose the protected method of its inner class as shown below.

public class ReloadableResourceBundleMessageSourceExt extends ReloadableResourceBundleMessageSource {

public Properties getPropertiesByFileName(String fileName){
    return super.getProperties(fileName).getProperties();
}

}

Third: Now Autowired the bean like this.

@Service 
public class MyMessagesBundleService {

final private String fileName = "classpath:messages";

@Autowired
ReloadableResourceBundleMessageSourceExt messageSource;
Properties properties = messageSource.getPropertiesByFileName(this.fileName);
// now change the properties and saved it.
// after saved call clear cache and get again.
messageSource.clearCache();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM