I believe you mixed two concepts:
- property files
- message resource bundles
Property files contains properties (locale independent). In Spring they can be loaded for example via util:properties
and can be used in @Value
annotations.
But message resource bundles (which are bases on files that look like property files) are language dependend. In Spring you can load them via org.springframework.context.support.ResourceBundleMessageSource
. But not inject in a String via @Value
. You can not inject them because the @Value
injection is done once per bean, the @Value
will be evaluated once (most at start time), and the calculated value will be injected. But this is not what you normally need when you use Message Resource Bundles. Because then you need to evaluate the value every time the variable is used, depending on the language of an user.
But you can build it easely by your own!
The only things you need is this class:
import java.util.Locale;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
@Configurable
public class MSG {
private String key;
@Resource(name = "messageSource")
private MessageSource messageSource;
public MSG(String key) {
super();
this.key = key;
}
public String value() {
Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage(key, new Object[0], locale);
}
@Override
public String toString() {
return value();
}
}
Then you can use it in this way:
@Service
public class Demo {
@Value("demo.output.hallo")
private MSG hallo;
@Value("demo.output.world")
private MSG world;
public void demo(){
System.out.println("demo: " + hello + " " + world);
}
}
To get it running, you need to enable <context:spring-configured />
to turn on AspectJ @Configurable support, and (That is importent) you need to instanciate the Ressouce Bundle Message Source in the same application context (for example in web apps you put the ReloadableResourceBundleMessageSource
definition in most cases in the web app context, but this does not work in this case, because the MSG object is in the "normal" application context.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…