【发布时间】:2014-09-21 16:11:06
【问题描述】:
我有一个正在运行的 Spring MVC 项目,并希望将应用程序上下文配置从 xml 迁移到 Java-Config。除 messageSource Bean 外,一切正常。
以下工作正常:
这个配置类被另一个配置类导入:
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>
以下操作不起作用:
将 bean 移动到 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/resources 中,并命名为 messages_en.properties。
Edit2:完整的项目源代码可以在这里找到:https://github.com/Katharsas/GMM/tree/PerfRevamp
【问题讨论】:
-
抱歉,当我将代码复制到问题中时,出于某种原因,我删除了该行。读了它。我还尝试了明确的 bean 命名,但没有帮助。
标签: java xml spring spring-java-config