Spring_ Internationalization Information Settings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.