MichelReap
MichelReap

Reputation: 5770

Injecting ResourceBundle in Spring Bean

I want to inject a resource bundle into a 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:

@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.

I.e. 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.

Upvotes: 4

Views: 12247

Answers (2)

Ry.xn
Ry.xn

Reputation: 192

I came across the same situtation as OP did, and M. Deinum 's comment to wrap MessageSource with MessageSourceResourceBundle solved my issue.

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));

Upvotes: 2

Ken Bekov
Ken Bekov

Reputation: 14015

Possible this part of documentation will be helpful. In your beans you should to use MessageSource. In controller, or service or any other bean you can use it next way:

@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):

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

...

Now about configuration. I would advice you to keep default id of ResourceBundleMessageSource bean. Default id is 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. But most of templaters (JSP, Thymeleaf and others) will be looking for messageSource bean, by default. 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.

Upvotes: 1

Related Questions