Spring ReloadableResourceBundleMessageSource bean

2019-04-15 06:56发布

问题:

I've tried to use the Mkyong's guide to access external properties file, without any success.

This is my bean definition in web-osgi-context.xml file located in WEB-INF:

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
    <list>
        <value>classpath:bundles/resource</value>
        <value>classpath:bundles/override</value>
        <value>file:c:/test/messages</value>
    </list>
    </property>
    <property name="cacheSeconds" value="10"/>      
</bean>

Accessing the bean:

@SpringBean
private ReloadableResourceBundleMessageSource messageSource;

Trying to extract the message like this:

String name = messageSource.getMessage("customer.name",
        new Object[] { 28,"http://www.mkyong.com" }, Locale.US);

System.out.println("Customer name (English) : " + name);

I have messages_en_US.properties files in both C:/test/messages and C:/test folders. They contain following line:

customer.name=Test, age : {0}, URL : {1}

That's all I have, am I missing something? The message I get is:

org.springframework.context.NoSuchMessageException: No message found under code 'customer.name' for locale 'en_US'.
 at org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:155)

Btw, I also tried internal properties, also without success. I deploy my .war in my local servicemix(6.1.1), I also use wicket (6.24.0) and spring (3.2.14). Running mkyong's application(which is not a web application) locally (without deploying it on my local servicemix works).

回答1:

Sooo I somewhat found the problem and got a workaroud..

This didn't work:

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename">
        <value>classpath:messages</value>
    </property>
</bean>

And in Code:

@SpringBean
private ReloadableResourceBundleMessageSource messageSource;

By running getClass() on this source I get

class WICKET_org.springframework.context.support.ReloadableResourceBundleMessageSource$$EnhancerByCGLIB$$852b0c02

But this is working:

@Bean
public MessageSource messageSource() {
     ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
     messageSource.setBasename("classpath:messages");
     return messageSource;
}

By running getClass() on this source I get

class org.springframework.context.support.ReloadableResourceBundleMessageSource

Is it possible that Cglib enhancer is messing thigs up here? Any way I can make first option working knowing that?