最近美国那边的工程师提了个问题,关于 ResourceBundleMessageSource初始化的问题,
HLD的详情是这样的,他想使用ResourceBundleMessageSource这个对象在Java code里面实现国际化,(Java code是一个Helper class,不继承任何类,用于jsp显示辅助,为让jsp干净做的),但是怎么得到ResourceBundleMessageSource这个对象呢,他给的HLD的这样的:
For access in Java: with the above bean.xml, any of the Spring ResourceBundleMessageSource methods will find and use the named bundle. Those Spring methods can be used directly (upon Getting Locale as above) – for example:
org.springframework.context.support.
*
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
String ResourceBundleMessageSource.getMessage(String key
//
message key
String[],
//
message args
String,
//
default value
Locale)
引用原文,
于是我就开始寻找怎么得到ResourceBundleMessageResource如何实例化,大家都知道ResourceBundleMessageResource是在国际化的时候使用的,我们通常在spring的bean.xml里面这样配置来加载我们的资源文件:
<
bean
id
="messageSource"
class
="org.springframework.context.support.ResourceBundleMessageSource"
>
<
property
name
="basenames"
>
<
list
>
<
value
>
Messages
</
value
>
</
list
>
</
property
>
</
bean
>
Spring IOC会帮我们管理ResourceBundleMessageResource对象的实例话和资源文件的加载.
也就是说在我们的应用启动的时候这些文件已经被加载了,对象已经被实例化了,我们要怎么拿到这个对象,使得我们在java code里面可以使用这个对象呢?
查遍了所有的Spring source code没有发现好的方法,可以这样做么?
ResourceBundleMessageSource ms
=
new
ResourceBundleMessageSource();
// Actually, This object had been created when the bean.xml is loaded.
//Shall I should create another?
//
not singleton? Too many object will be created?
ms
HLD的详情是这样的,他想使用ResourceBundleMessageSource这个对象在Java code里面实现国际化,(Java code是一个Helper class,不继承任何类,用于jsp显示辅助,为让jsp干净做的),但是怎么得到ResourceBundleMessageSource这个对象呢,他给的HLD的这样的:
For access in Java: with the above bean.xml, any of the Spring ResourceBundleMessageSource methods will find and use the named bundle. Those Spring methods can be used directly (upon Getting Locale as above) – for example:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
引用原文,
于是我就开始寻找怎么得到ResourceBundleMessageResource如何实例化,大家都知道ResourceBundleMessageResource是在国际化的时候使用的,我们通常在spring的bean.xml里面这样配置来加载我们的资源文件:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
也就是说在我们的应用启动的时候这些文件已经被加载了,对象已经被实例化了,我们要怎么拿到这个对象,使得我们在java code里面可以使用这个对象呢?
查遍了所有的Spring source code没有发现好的方法,可以这样做么?
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
// Actually, This object had been created when the bean.xml is loaded.
//Shall I should create another?
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)