1

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

рüффп
  • 5,172
  • 34
  • 67
  • 113
SSV
  • 860
  • 3
  • 11
  • 25
  • So it seems the problem is ServiceMix. I'd add it in the tags and hope someone with ServiceMix experience would help. – martin-g Oct 07 '16 at 09:14
  • Well, I think it's mostly because it's a web application, not just string application. Haven't tested mkyongs approach locally in web application – SSV Oct 07 '16 at 09:24

1 Answers1

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?

SSV
  • 860
  • 3
  • 11
  • 25
  • Wicket uses CGlib to create a proxy around the Spring bean, but any method calls on that proxy are delegated to the real bean. So the real bean does the logic to find the message, not the proxy. Also the location is the root of the classpath, so it doesn't really matter which class is used. – martin-g Oct 07 '16 at 12:33
  • As I suggested in the question's comments: the problem is ServiceMix. It is OSGi based and classpaths and classloaders behave differently there than in a normal servlet web server. – martin-g Oct 07 '16 at 12:34
  • If there is no difference between which one is used then why the second one works for me and first doesn't? – SSV Oct 07 '16 at 13:51