通过Spring Framework中的注释从resourceBundle获取本地化消息

lis*_*sak 15 java spring annotations localization resourcebundle

是否有可能做到这一点 ?目前这样做是这样的:

<bean id="resource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>content.Language</value> 
        </list>
    </property>
</bean>

@Autowired
protected MessageSource resource;

protected String getMessage(String code, Object[] object, Locale locale) {
    return resource.getMessage(code, object, locale);
}
Run Code Online (Sandbox Code Playgroud)

有没有办法通过@Value注释获取属性?

<util:properties id="generals" location="classpath:portlet.properties" />

    @Value("#{generals['supported.lang.codes']}")
    public String langCodes;
Run Code Online (Sandbox Code Playgroud)

因为必须调用该方法通常很好,但是例如在单元测试时,这很痛苦......在某些情况下,webdriver的PageObject模式对象没有初始化,这将是非常有帮助的

Ral*_*lph 28

我相信你混合了两个概念:

  • 属性文件
  • 消息资源包

属性文件包含属性(与语言环境无关).在Spring中,它们可以例如通过加载util:properties并可以在@Value注释中使用.

但是消息资源包(基于看起来像属性文件的文件)是语言依赖的.在Spring中,您可以通过它们加载它们org.springframework.context.support.ResourceBundleMessageSource.但是不要通过String注入@Value.你不能注射它们,因为@Value每个bean注射一次,@Value将被评估一次(大多数在开始时间),并且将注入计算值.但这不是您使用Message Resource Bundles时通常需要的.因为每次使用变量时都需要评估值,具体取决于用户的语言.


但是你可以自己轻松地构建它!

你需要的唯一的东西就是这堂课:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以这样使用它:

@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);
    }    
}
Run Code Online (Sandbox Code Playgroud)

要使其运行,您需要启用<context:spring-configured />AspectJ @Configurable支持,并且(这是重要的)您需要在同一应用程序上下文中实现Ressouce Bundle消息源(例如,ReloadableResourceBundleMessageSource在大多数情况下您定义的Web应用程序中)在Web应用程序上下文中,但在这种情况下这不起作用,因为MSG对象位于"正常"应用程序上下文中.


lis*_*sak 5

关键是这仅对单元测试有用。在实际应用中,Locale是一个运行时信息,不能硬编码在注释中。区域设置是根据运行时中的用户区域设置决定的。

顺便说一句,您可以轻松地自己实现这一点,例如:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Localize {

    String value();

}
Run Code Online (Sandbox Code Playgroud)

public class CustomAnnotationBeanPostProcessor implements BeanPostProcessor {

    public Object postProcessAfterInitialization(Object bean, String beanName) {
        return bean;
    }

    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        Class clazz = bean.getClass();
        do {
            for (Field field : clazz.getDeclaredFields()) {
                if (field.isAnnotationPresent(Localize.class)) {
                    // get message from ResourceBundle and populate the field with it
                }
            }
            clazz = clazz.getSuperclass();
        } while (clazz != null);
        return bean;
    }
Run Code Online (Sandbox Code Playgroud)