我有一個可用的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
你忘了'@ Bean'。 – axtavt 2014-09-21 16:32:07
對不起,我將代碼複製到問題時出於某種原因刪除了該行。讀過它。我也嘗試了顯式的bean命名,這沒有幫助。 – Katharsas 2014-09-21 16:39:01