2014-09-21 44 views
2

我有一個可用的Spring MVC項目,並且希望將應用程序上下文配置從xml遷移到Java-Config。除了messageSource Bean之外,所有的工作都很好。Spring messageSource只能用作xml(不像Spring-Java-Config)


工作正常
這個配置類獲取由另一個配置一流的進口:

package gmm; 

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

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

} 

參考applicationContext.xml文件:

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

下列情況不行
感動豆成Java的配置:

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

當我使用這個java的配置,我得到的只是平常???key.for.message???東西。調試輸出不會告訴我一些不尋常的事情。
我不明白這裏有什麼問題。我的代碼中是否有一些明顯的錯誤?請告訴我,即使你現在沒有解決方案,因爲我現在覺得我有點笨!這應該是超級容易的,不是嗎?


編輯:消息文件是在的src/main /資源和被命名爲喜歡messages_en.properties
編輯2:完整的項目源代碼可以在這裏找到:https://github.com/Katharsas/GMM/tree/PerfRevamp

+0

你忘了'@ Bean'。 – axtavt 2014-09-21 16:32:07

+0

對不起,我將代碼複製到問題時出於某種原因刪除了該行。讀過它。我也嘗試了顯式的bean命名,這沒有幫助。 – Katharsas 2014-09-21 16:39:01

回答

2

好的,我解決了這個問題!

TLDR:我有Java-Config在另一個文件中定義了一個messageSource Bean(我沒有注意到)。該配置確實覆蓋了我的Java-Config,但無法覆蓋xml配置。所以這個XML工作,但Java-Config沒有。

我如何發現錯誤:

我只是複製兩個版本的servlet的啓動日誌到在線文本比較工具

工作代碼日誌(與虛擬文本替換時間戳後):

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]] 

不工作的代碼相同的行:

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. 
相關問題