Spring messageSource only works as xml (not as Spring-Java-Config)



I have a working Spring MVC Project and want to migrate the application context configuration from xml to Java-Config. All works fine except messageSource Bean.




Following works fine:

This config class gets imported by another config class:



package gmm;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource({"classpath:applicationContext.xml"})
public class I18nConfiguration {

}


Referenced applicationContext.xml file:



<beans
xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:context="http://ift.tt/GArMu7"
xsi:schemaLocation="
http://ift.tt/GArMu6
http://ift.tt/18sW2ax
http://ift.tt/GArMu7
http://ift.tt/1bGeTcI
">

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

</beans>




Following does not work:

Moved bean into java config:



package gmm;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

@Configuration
public class I18nConfiguration {

public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename("messages");
return source;
}
}


When i use this java config, i get just the usual ???key.for.message??? stuff. Debugging output doesn't tell me something unusual.

I don't see what is wrong here. Is there some obvious error in my code? Please tell me even if you don't now the solution cause i feel like Im kind of dumb right now! This is supposed to be super easy isn't it?




Edit: The message files are in src/main/resources and are named like messages_en.properties.


No comments:

Post a Comment