首先,在Spring的application.xml中定义

 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 国际化信息所在的文件名 -->
<property name="basename" value="messages/messages"/>
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name="useCodeAsDefaultMessage" value="true"/>
</bean>

其中,id 的值必须是 “messageSource”,否则会报错

首先,我想到的是,既然它是一个被声明好的bean,那么,应该可以使用 @Autowired 标签来绑定吧。于是我写了如下的代码:

public class Const {
@Autowired
private static ResourceBundleMessageSource rms public static String getTextValue(String key) {
return rms.getMessage(key, null, null);
}
}

rms 的值一直是null,也就是说注入失败了

后来在网上查到,“ApplicationContext” 这个接口继承了“MessageSource”接口,那么我们只要获取项目的 ApplicationContext 的实现类,就可以通过 getMessage() 方法来获取国际化文件内容了。

那么要如何简单方便的来获取 ApplicationContext 的实现类呢?这个时候就需要另一个接口了,即“ApplicationContextAware”,任何类实现这个接口,均会被注入  ApplicationContext 。

public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; public static ApplicationContext getApplicationContext() {
return applicationContext;
} @Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
applicationContext = arg0;
} public static Object getBean(String id) {
Object object = null;
object = applicationContext.getBean(id);
return object;
}
}

当然,必须要将上面的 SpringUtil 类在application.xml文件中配置一下,才能让它被spring框架读取,然后给它注入 ApplicationContext。配置很简单:

<bean id="SpringUtil" class="util.SpringUtil"/>  

这样就行了

public class Const {  

    public static String getTextValue(String key) {
return SpringUtil.getApplicationContext().getMessage(key, null, null);
}
}

spring mvc中,如何在 Java 代码里,获取 国际化 内容的更多相关文章

  1. Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)

    <bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...

  2. Spring Mvc中Jsp也页面怎么会获取不到Controller中的数据

    ----------Controller ------- package com.test.mvc; import org.springframework.stereotype.Controller; ...

  3. spring MVC 管理HttpClient---实现在java中直接向Controller发送请求

    在spring MVC中,大多数时候是由客户端的页面通过ajax等方式向controller发送请求,但有时候需要在java代码中直接向controller发送请求,这时可以使用HttpCilent实 ...

  4. Android如何在java代码中设置margin

    习惯了直接在xml里设置margin(距离上下左右都是10dip),如: <ImageView android:layout_margin="10dip" android:s ...

  5. 转--Android如何在java代码中设置margin

    ========  3 在Java代码里设置button的margin(外边距)? 1.获取按钮的LayoutParams LinearLayout.LayoutParams layoutParams ...

  6. 如何在Java代码中使用SAP云平台CloudFoundry环境的环境变量

    本文使用的例子源代码在我的github上. 在我的公众号文章在SAP云平台的CloudFoundry环境下消费ABAP On-Premise OData服务介绍了如何通过Cloud Connector ...

  7. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  8. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  9. Spring MVC中的HandlerMapping与HandlerAdapter

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

随机推荐

  1. SqlProfiler的替代品-ExpressProfiler

    可以用来跟踪执行的sql语句.安装SqlServer之后SqlServerManagementStudio自带一个SqlProfiler,但是如果安装的SqlExpress,那就没有了. 项目的主页在 ...

  2. Python3.6全栈开发实例[027]

    27.文件a.txt内容:每一行内容分别为商品名字,价钱,个数.apple 10 3tesla 100000 1mac 3000 2lenovo 30000 3chicken 10 3通过代码,将其构 ...

  3. python面向对象(二)

    属性查找 类有两种属性:数据属性和函数属性 1. 类的数据属性是所有对象共享的 2. 类的函数属性是绑定给对象用的 class BeijingStudent:   school='Beijing'  ...

  4. 非Linux环境下调用sh命令

    方法一:把cygwin的bin配置到环境变量里,这样做了以后在cmd.exe里也可以使用linux的命令 def exe_command(command): p = subprocess.Popen( ...

  5. 江卓尔与比特币增发,谣言or先知-千氪

    最近,很多圈内人都在讨论比特币是否应该增发,但等等,比特币真的会增发吗?到底是真有增发计划还是某些人别有用心地在散布谣言? 那么消息是从哪里出来的?北京时间 2 月 10 日晚,莱比特矿池创始人江卓尔 ...

  6. R语言(一)

    向量运算 R的强大功能之一就是把整个数据向量作为一个单一对象来处理.一个数据向量仅是数字的排列,一个向量可以通过如下方式构造 weight<-c(,,,) weight [] 结构c(--)用来 ...

  7. BADI:LE_SHP_DELIVERY_PROC-增强在交货处理中

    1.所得方法清单: CHANGE_FCODE_ATTRIBUTES Control Activation of Function CodesCHANGE_FIELD_ATTRIBUTES Contro ...

  8. English Phrases

    @1:Phrases requst sth from/of sb 向某人要求某物 a new lease on life   重获新生.焕发生机 state of the art 最先进的 at th ...

  9. maven 项目打包时无法解析读取properties文件

    在做项目时遇见一个问题,无法解析properties文件的 内容 异常为 Could not resolve placeholder ......... 在此之前均有做相关的 配置 但是从未出现过如上 ...

  10. range开始节点和结束节点

    sc = range.startContainer, so = range.startOffsest ec = range.endContainer, eo=range.endOffset   sta ...