简体   繁体   English

在Spring Bean中注入ResourceBundle

[英]Injecting ResourceBundle in Spring Bean

I want to inject a resource bundle into a bean. 我想将资源束注入bean。 I need the resourcebundle, I can't get the messages directly. 我需要资源束,我无法直接获取消息。 I am using this snippet to load the resource: 我正在使用此代码段加载资源:

 <bean id="reportMessages" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename">
            <value>report.messages</value>
        </property>

Then I can inject it into my bean like this: 然后可以将其注入到我的bean中,如下所示:

@Autowired
@Qualifier("reportMessages")
private ResourceBundleMessageSource reportMessages;

But this gives me a ResourceBundleMessageSource, which has a getResourceBundle() method which is protected and thus I can't call it. 但这给了我一个ResourceBundleMessageSource,它具有受保护的getResourceBundle()方法,因此我不能调用它。

Ie what I want is Spring's built-in functionality to read a Message Bundle depending on locale, and then treat it as a separate bean. 即我想要的是Spring的内置功能,它可以根据语言环境读取消息包,然后将其视为独立的bean。

Possible this part of documentation will be helpful. 部分文档可能会有所帮助。 In your beans you should to use MessageSource . 在您的bean中,您应该使用MessageSource In controller, or service or any other bean you can use it next way: 在控制器,服务或任何其他bean中,您可以按以下方式使用它:

@Controller
public class MyController{

    @Autowired
    private MessageSource messageSource;

    ....

    @RequestMapping("/messages")
    public String showMessages(ModelMap model) {

        String englishMessage = messageSource.getMessage("commend.message", null, 
            new Locale("en", "US"));
        String russianhMessage = messageSource.getMessage("commend.message", null, 
            new Locale("ru", "RU"));
        ...
    }
}

And in view (if you use JSP, of cause): 并且鉴于(如果使用JSP,则是原因):

<div>
    <span>
        <spring:message code="commend.message"/>
    </span>
</div>

...

Now about configuration. 现在介绍配置。 I would advice you to keep default id of ResourceBundleMessageSource bean. 我建议您保留ResourceBundleMessageSource bean的默认ID。 Default id is messageSource : 默认ID为messageSource

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
     <property name="basename">
          <value>report.messages</value>
     </property>
</bean>

Of cause, you can autowire this bean by @Qualifier annotation, as you did. 当然,您可以像以前一样通过@Qualifier注释自动装配该bean。 But most of templaters (JSP, Thymeleaf and others) will be looking for messageSource bean, by default. 但是默认情况下,大多数模板设计人员(JSP,Thymeleaf等)都将寻找messageSource bean。 So if you'll keep default name, you will not need to change settings of template engines. 因此,如果您保留默认名称,则无需更改模板引擎的设置。

Don't forget to put in root of classpath of your application property files with messages for every needed language. 不要忘记在应用程序属性文件的类路径的根目录中放入每种所需语言的消息。 In this example it will report.messages.properties (default), report.messages_en_US.properties and report.messages_ru_RU.properties . 在此示例中,它将为report.messages.properties (默认值), report.messages_en_US.propertiesreport.messages_ru_RU.properties

I came across the same situtation as OP did, and M. Deinum 's comment to wrap MessageSource with MessageSourceResourceBundle solved my issue. 我遇到了与OP相同的情况,M。Deinum的用MessageSourceResourceBundle包装MessageSource的评论解决了我的问题。

Locale locale = Locale.getDefault();
params.put(JRParameter.REPORT_LOCALE, locale);
    /* wrap the annotated messageSource with MessageSourceResourceBundle */
params.put(JRParameter.REPORT_RESOURCE_BUNDLE, new MessageSourceResourceBundle(messageSource, locale));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM