Life is too short, today give up tomorrow not necessarily can get.
Spring has classes that have multiple sources of information, both of which implement the Messagesource interface. Among these classes, the most common and useful are Resourcebundlemessagesource and
Reloadableresourcebundlemessagesource .
1. Configure Resourcebundlemessagesource
1) Use Java configuration
Packageconfig;ImportOrg.springframework.context.MessageSource;ImportOrg.springframework.context.annotation.Bean;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.context.support.ResourceBundleMessageSource;ImportOrg.springframework.web.servlet.ViewResolver;ImportORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;ImportOrg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;ImportOrg.springframework.web.servlet.view.InternalResourceViewResolver;ImportOrg.springframework.web.servlet.view.JstlView, @Configuration @enablewebmvc@componentscan ("Study.practice") Public classWebconfigextendsWebmvcconfigureradapter {/*** Configure JSP View parser: Internalresourceviewresolver *@return */@Bean Publicviewresolver Viewresolver () {internalresourceviewresolver resolver=NewInternalresourceviewresolver (); //Add prefixResolver.setprefix ("/web-inf/views"); //Add suffixResolver.setsuffix (". JSP"); Resolver.setviewclass (Jstlview.class); returnResolver; } /*** Configure internationalized information source Resourcebundlemessagesource *@return */@Bean PublicMessagesource Messagesource () {Resourcebundlemessagesource Messagesource=NewResourcebundlemessagesource (); Messagesource.setbasename ("Messages"); returnMessagesource; }}
2) Using XML configuration
<id= "Messagesource" class= " Org.springframework.context.support.ResourceBundleMessageSource " p:basename=" Messages " />
Setting the Bean's BaseName property,Resourcebundlemessagesource will attempt to parse the information in the root path's properties file, the names of these properties are derived from this base name, If the above code basename is ' messages ', then these attribute files can be named ' messages_zh.properties ' or ' messages_us.properties '.
2. Configure Reloadableresourcebundlemessagesource
1) Use Java configuration
@Bean Public Messagesource Messagesource () { = new reloadableresourcebundlemessagesource (); Messagesource.setbasename ("file:///etc/source/messages"); Messagesource.setcacheseconds (ten); return messagesource; }
2) Using XML configuration
<id= "Messagesource" class= " Org.springframework.context.support.ReloadableResourceBundleMessageSource " p:basename=" File:///etc/source/messages " p:cacheseconds=" Ten " />
The two scenarios work very much the same way, except that Reloadableresourcebundlemessagesource can reload information properties without having to recompile or restart the app.
Spring_ Internationalization Information Settings