首先,在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. SPDY

    转载SPDY 是什么 SPDY 是 Google 开发的基于传输控制协议 (TCP) 的应用层协议 ,开发组正在推动 SPDY 成为正式标准(现为互联网草案).SPDY 协议旨在通过压缩.多路复用和优 ...

  2. Python Interpreter

    在开始之前,我们先限定下python解释器的意思.当讨论Python的时候,解释器这个词可以用在不同的地方.有的时候,解释器指的是Python Interpreter,也就是你在命令行交互界面上输入p ...

  3. nodejs获取服务器数据到页面

    const Koa = require('koa'); const Router = require('koa-router'); const app = new Koa(); const route ...

  4. SaltStack远程执行命令

    编辑fansik_cmd.sls文件: 内容如下: fansik_cmd:  cmd.run:    - unless:      - test -f /tmp/fansik.txt      - t ...

  5. android各种组件的监听器

    <一>Spinner(旋转按钮或下拉列表):设置监听器为:setOnItemSelectedListener 设置动画效果为:setOnTouchListener              ...

  6. php备份mysql数据库

    <?php /*程序功能:mysql数据库备份功能*/ ini_set('max_execution_time','0'); ini_set('memory_limit','1024M');// ...

  7. asp.net,缓存Cache

    缓存Cache: >直接使用Cache["content"],缓存与Session不同,所有用户都可以共享.永不过期,由服务器自己维护,当内存不够时,会将老的缓存释放掉. & ...

  8. springboot打war包

    修改pom为war不是jar. 移除tomcar的jar依赖: <dependency> <groupId>org.springframework.boot</group ...

  9. Linux用户和用户组管理 用户组管理命令

    添加用户组命令:groupadd 命令格式: [root@localhost ~]# groupadd [选项] 组名 选项: 选项 选项说明 -g GID 指定组ID: 修改用户组命令:groupm ...

  10. Go Redis 开发

    redigo库来实现redis的操作:https://github.com/gomodule/redigo Redis常用操作 示例代码: package main import ( "gi ...