Java ResourceBundle在spring中作为PropertySource
PropertySource演示了如何使用Java ResourceBundle在spring中作为PropertySource,java,spring,Java,Spring,PropertySource演示了如何使用@PropertySource将属性文件键/值对注入到spring上下文中 我想知道是否可以对资源包执行同样的操作。例如: @Configuration @PropertySource("classpath:configuration") public class FooBar { @Value("${my.key}") private String someValue; public void printMe(){
@PropertySource
将属性文件键/值对注入到spring上下文中
我想知道是否可以对资源包执行同样的操作。例如:
@Configuration
@PropertySource("classpath:configuration")
public class FooBar {
@Value("${my.key}")
private String someValue;
public void printMe(){
System.out.println("my.key = " + someValue);
}
}
配置.properties
配置属性
FooBar实例上的printMe结果将打印字符串:
my.key = baz
如果
user.language
系统属性设置为en
嗨,您可以尝试使用@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()));
}
@Resource(name=”“)
受保护的消息源资源;
public void printMe(){
System.out.println(resources.getMessage(“my.key”,null,LocaleContextHolder.getLocale());
}
是的,在需要国际化的情况下,您也可以这样做。因此,我的问题是正确的?@Value
s在启动时处理,而不是在运行/执行时处理。因此,不可以加载区域设置敏感的属性文件。如果需要,您必须使用MessageSource
并在每次需要时手动检索该值。
my.key = baz
@Resource(name = "<id of your configured message source>")
protected MessageSource resources;
public void printMe() {
System.out.println(resources.getMessage("my.key", null, LocaleContextHolder.getLocale()));
}