Reputation: 560
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://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
">
<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 {
@Bean
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.
Edit2: The full project source code can be found here: https://github.com/Katharsas/GMM/tree/PerfRevamp
Upvotes: 2
Views: 7984
Reputation: 560
Okay, I solved this!
TLDR: I had Java-Config defining a messageSource Bean in another file (which i wasn't aware off). That config did override my Java-Config, but could not override the xml config. So the xml worked, but Java-Config didn't.
How i found that error:
I just copied the servlet startup log of both versions into an online text diff tool (after replacing timestamps with dummy text)
Working code log:
INFORMATION: Loading XML bean definitions from class path resource [applicationContext.xml]
19 Sep 25, 2014 time org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
20 INFORMATION: Overriding bean definition for bean 'messageSource': replacing [Generic bean: class [com.technologicaloddity.capturejsp.util.TechOddMessageSource]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\Users\Jan\Repositories\GMM\target\classes\com\technologicaloddity\capturejsp\util\TechOddMessageSource.class
]] with [Generic bean: class [org.springframework.context.support.ResourceBundleMessageSource]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [applicationContext.xml]]
Not working code same line:
Sep 25, 2014 time org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader isOverriddenByExistingDefinition
INFORMATION: Skipping bean definition for [BeanMethod:name=messageSource,declaringClass=gmm.I18nConfiguration]: a definition for bean 'messageSource' already exists. This top-level bean definition is considered as an override.
Upvotes: 2