ResourceBundle作为PropertySource在春天

时间:2014-06-11 07:07:48

标签: java spring

PropertySource documentation演示如何使用@PropertySource将属性文件键/值对注入弹簧上下文。

我想知道是否可以对资源包执行相同的操作。例如:

@Configuration
@PropertySource("classpath:configuration")
public class FooBar {
    @Value("${my.key}")
    private String someValue;

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

configuration.properties

my.key=foo

configuration_en.properties

my.key=baz

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

my.key = baz

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

1 个答案:

答案 0 :(得分:0)

您好,您可以尝试使用@Resource

注释您的消息来源
    @Resource(name = "<id of your configured message source>")
    protected MessageSource resources;

    public void printMe() {
            System.out.println(resources.getMessage("my.key", null, LocaleContextHolder.getLocale())); 
    }