Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring消息源,BeanXML中的getMessage?_Java_Spring - Fatal编程技术网

Java Spring消息源,BeanXML中的getMessage?

Java Spring消息源,BeanXML中的getMessage?,java,spring,Java,Spring,我的资源设置如下: <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename"> <value>locale\\messages</value> </property> </

我的资源设置如下:

<bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename">
            <value>locale\\messages</value>
        </property>
</bean>
当我使用bean时,我希望看到文本“TestBattle”:

<bean id="battlefield" class="com.mypackage.Battlefield" scope="prototype">
    <constructor-arg index="0" value="battle.name" />
    <constructor-arg index="1" ref="armies" />
</bean>

用java编写代码?

至少,您可以使用spel来完成

比如说

<bean id="messageSourceAccessor" class="org.springframework.context.support.MessageSourceAccessor">
    <constructor-arg ref="messageSource" />
</bean>

<bean id="battlefield" class="com.mypackage.Battlefield" scope="prototype">
    <constructor-arg index="0" value="#{messageSourceAccessor.getMessage('battle.name')}" />
    <constructor-arg index="1" ref="armies" />
</bean>
并使用前缀i18n:翻译代码,即

<bean id="propertyEditorConfigure" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="propertyEditorRegistrars">
        <list>
            <bean class="message.MessageEditorRegistrar">
                <property name="messageSource" ref="messageSource" />
            </bean>
        </list>
    </property>
</bean>

<bean id="battlefield" class="com.mypackage.Battlefield" scope="prototype">
    <constructor-arg index="0" value="i18n:battle.name" />
    <constructor-arg index="1" ref="armies" />
</bean>

getMessage("battle.name",...
<bean id="messageSourceAccessor" class="org.springframework.context.support.MessageSourceAccessor">
    <constructor-arg ref="messageSource" />
</bean>

<bean id="battlefield" class="com.mypackage.Battlefield" scope="prototype">
    <constructor-arg index="0" value="#{messageSourceAccessor.getMessage('battle.name')}" />
    <constructor-arg index="1" ref="armies" />
</bean>
public class MessageSourcePropertyEditor extends PropertyEditorSupport {

    private MessageSourceAccessor messageSourceAccessor;

    public MessageSourcePropertyEditor(MessageSource messageSource) {
        this.messageSourceAccessor = new MessageSourceAccessor(messageSource);
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        String value = text;

        if (text.startsWith("i18n:")) {
            value = messageSourceAccessor.getMessage(text.substring(5));
        }

        setValue(value);
    }
}

public class MessageEditorRegistrar implements PropertyEditorRegistrar {

    private MessageSource messageSource;

    @Override
    public void registerCustomEditors(PropertyEditorRegistry registry) {
        registry.registerCustomEditor(String.class, new MessageSourcePropertyEditor(messageSource));

    }

    public MessageSource getMessageSource() {
        return messageSource;
    }

    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }
}
<bean id="propertyEditorConfigure" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="propertyEditorRegistrars">
        <list>
            <bean class="message.MessageEditorRegistrar">
                <property name="messageSource" ref="messageSource" />
            </bean>
        </list>
    </property>
</bean>

<bean id="battlefield" class="com.mypackage.Battlefield" scope="prototype">
    <constructor-arg index="0" value="i18n:battle.name" />
    <constructor-arg index="1" ref="armies" />
</bean>