This article reproduced only for their own study included, do not do any commercial use, if necessary, please visit the original address: http://blog.csdn.net/qyf_5445/article/details/8124431
ApplicationContext
Interfaces extend the MessageSource
interface, thus providing the ability to process messages (i18n or internationalization). HierarchicalMessageSource
together with it, it is also able to handle nested messages, which are the basic interfaces that spring provides for processing messages. Let's take a quick look at the method it defines:
-
string getMessage (String code, object[] args, string default, Locale Loc) : The basic method for getting messages from the messagesource
. If no message is found in the specified locale, the default message is used. The parameters in args will use the messageformat
in the standard class library to replace the values in the message.
-
string getMessage (String code, object[] args, Locale Loc): Essentially the same as the previous method, The difference is that no default value is specified, and if no message is found, a nosuchmessageexception
exception is thrown.
-
string getMessage (messagesourceresolvable resolvable, locale locale) /code>: The properties used in the above methods are encapsulated in a messagesourceresolvable
implementation, and this method can specify messagesourceresolvable
implementation.
When one ApplicationContext
is loaded, it automatically looks in the context for a bean that has been defined as a MessageSource
type. The name of this bean must be messageSource
. If found, then all calls to the above method will be delegated to the bean. Otherwise ApplicationContext
, it will look in its parent class for a bean with the same name. If there is, take it as MessageSource
. If it does not eventually find any source of the message, an empty one StaticMessageSource
will be instantiated so that it can accept the call of the above method.
Spring currently offers two MessageSource
implementations: ResourceBundleMessageSource
and StaticMessageSource
. They are inherited NestingMessageSource
so that nested messages can be processed. StaticMessageSource
rarely used, but can programmatically add messages to a message source. ResourceBundleMessageSource
will be used a bit more, for example:
<beans> <bean id= "Messagesource" class= " Org.springframework.context.support.ResourceBundleMessageSource "> <property name=" Basenames "> <list> <value>format</value> <value>exceptions</value> <value> windows</value> </list> </property> </bean></beans>
This configuration assumes that there are three resource files (resource bundles) in your classpath, and that they are format
, exceptions
and windows
. With ResourceBundle, any request to parse a message is processed using the standard way of parsing the message in the JDK. For the purposes of the example, assume that the contents of the above two resource files are ...
# in ' format.properties ' message=alligators rock!
# in ' exceptions.properties ' Argument.required=the ' {0} ' argument is required.
Here is the test code. Because ApplicationContext
implementations also implement MessageSource
interfaces, they can be transformed into MessageSource
interfaces
public static void Main (string[] args) { Messagesource resources = new Classpathxmlapplicationcontext ("Beans.xml"); String message = resources.getmessage ("message", NULL, "Default", null); SYSTEM.OUT.PRINTLN (message);}
The output of the above program will be ...
Alligators rock!
In summary, we ‘beans.xml‘
define a bean in the file (in the classpath root directory) messageSource
that basenames
references multiple resource files through its properties, and the basenames
property values are passed in by the three values specified by the list element. They exist in the form of files and are placed in the root directory of the classpath (respectively format.properties
, exceptions.properties
and windows.properties
).
Again, this time we will look at the message that passes the parameter to the lookup, which will be converted to a string and inserted into the placeholder in the found message: The number in the curly brackets in the resource file is the placeholder.
<beans> <!--this messagesource
is being used in a Web application-<bean id=" Messagesource "class=" org.sp Ringframework.context.support.ResourceBundleMessageSource "> <property name=" baseName "value=" web-inf/ Test-messages "/> </bean> <!--Let's inject the above messagesource
into this POJO-<bean id=" Example "class=" Com.foo.Example "> <prope Rty name= "Messages" ref= "Messagesource"/> </bean></beans>
public class Example { private messagesource messages; public void Setmessages (Messagesource messages) { this.messages = messages; } public void Execute () { String message = This.messages.getMessage ("argument.required", new Object [] {"Userdao" }, "Required", null); SYSTEM.OUT.PRINTLN (message);} }
execute()
the output of the calling method is ...
The ' Userdao ' argument is required.
For internationalization (i18n), the different implementations in spring are the MessageResource
same as the locale resolution rules in JDK standard resourcebundle. For example, the bean defined in the example above, messageSource
if you want to parse the British (EN-GB) locale message, then you need to create format_en_GB.properties
, exceptions_en_GB.properties
and windows_en_GB.properties
three resource files.
Locale parsing is typically specified by the application according to the running environment. For the purposes of the example, we manually specify the locale parameter value for the (British) message that will be processed.
# in ' exceptions_en_gb.properties ' Argument.required=ebagum lad, the ' {0} ' argument is required, I say, required.
public static void Main (final string[] args) { Messagesource resources = new Classpathxmlapplicationcontext (" Beans.xml "); String message = Resources.getmessage ("argument.required", new Object [] {"Userdao"}, "required", locale.uk); SYSTEM.OUT.PRINTLN (message);}
The output of the above program is ...
Ebagum Lad, the ' userdao ' argument is required, I say, required.
MessageSourceAware
The interface can also be used to get any defined MessageSource
references. Any MessageSourceAware
bean that implements the interface will be injected with it when it is created and configured MessageSource
.
Messagesource in spring 3--resourcebundlemessagesource "Go" using the configuration method