First, define in spring's application.xml
<BeanID= "Messagesource"class= "Org.springframework.context.support.ResourceBundleMessageSource"> <!--the file name where internationalization information resides - < Propertyname= "BaseName"value= "Messages/messages"/> <!--if the corresponding code is not found in the Internationalized resource file, use this code as the name - < Propertyname= "Usecodeasdefaultmessage"value= "true"/></Bean>
Where the value of the ID must be "messagesource" or an error will be
First of all, I think that since it is a well-declared bean, you should be able to use @Autowired tag to bind it. So I wrote the following code:
Public class Const { @Autowired privatestatic resourcebundlemessagesource RMS publicstatic string Gettextvalue (String key) { return NULLnull); } }
The RMS value is always null, which means that the injection failed.
Later on the internet, "ApplicationContext" This interface inherits the "Messagesource" interface, then we just get the project ApplicationContext implementation class, you can pass the GetMessage () method to get the Internationalized file content.
So how easy is it to get the ApplicationContext implementation class? This time requires another interface, that is, "Applicationcontextaware", any class implementation of this interface, will be injected into the applicationcontext.
Public classSpringutilImplementsApplicationcontextaware {Private StaticApplicationContext ApplicationContext; Public StaticApplicationContext Getapplicationcontext () {returnApplicationContext; } @Override Public voidSetapplicationcontext (ApplicationContext arg0)throwsbeansexception {ApplicationContext=arg0; } Public StaticObject Getbean (String ID) {Object object=NULL; Object=Applicationcontext.getbean (ID); returnobject; } }
Of course, the above Springutil class must be configured in the Application.xml file to allow it to be read by the spring framework and then inject applicationcontext into it. The configuration is simple:
<id= "Springutil" class= "Util". Springutil "/>
That's it, okay?
public class Const {public static string Gettextvalue (String key) { return Springutil.getapplicationcontext (). GetMessage (key, NULL, NULL); } }
In spring MVC, how to get internationalized content in Java code