【问题标题】:Spring messageSource only works as xml (not as Spring-Java-Config)Spring messageSource 仅作为 xml 工作(而不作为 Spring-Java-Config)
【发布时间】: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


【解决方案1】:

好的,我解决了这个问题!

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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2013-12-02
    • 1970-01-01
    相关资源
    最近更新 更多