首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ResourceBundle在春季成为PropertySource

ResourceBundle在春季成为PropertySource
EN

Stack Overflow用户
提问于 2014-06-11 07:07:48
回答 1查看 1.1K关注 0票数 0

PropertySource 文档演示了如何使用@PropertySource将属性文件键/值对注入spring上下文。

我想知道是否可以对资源包进行同样的处理。例如:

代码语言:javascript
复制
@Configuration
@PropertySource("classpath:configuration")
public class FooBar {
    @Value("${my.key}")
    private String someValue;

    public void printMe(){
        System.out.println("my.key = " + someValue); 
    }
}

configuration.properties

代码语言:javascript
复制
my.key=foo

configuration_en.properties

代码语言:javascript
复制
my.key=baz

printMe在FooBar实例上的结果将打印字符串:

代码语言:javascript
复制
my.key = baz

如果user.language系统属性设置为en

EN

回答 1

Stack Overflow用户

发布于 2014-06-25 02:37:25

嗨,您可以尝试用@Resource注释您的消息源

代码语言:javascript
复制
    @Resource(name = "<id of your configured message source>")
    protected MessageSource resources;

    public void printMe() {
            System.out.println(resources.getMessage("my.key", null, LocaleContextHolder.getLocale())); 
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24156688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档