Java 注入的MessageSource为空

Java 注入的MessageSource为空,java,spring,Java,Spring,框架:Spring 3. 我真的不明白为什么bean中的消息源injectend总是为NULL 以下是片段: servlet.xml <context:annotation-config /> <context:component-scan base-package="com.myproject.controllers" /> <mvc:annotation-driven /> <bean id="messageSource" class="or

框架:Spring 3.

我真的不明白为什么bean中的消息源injectend总是为NULL

以下是片段:

servlet.xml

<context:annotation-config />

<context:component-scan base-package="com.myproject.controllers" />

<mvc:annotation-driven />


<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages/messages" />
    <property name="cacheSeconds" value="0" />
</bean>
这是控制器

@Controller
@SessionAttributes("userSearchForm")
public class UsersController extends PaginationController<ProfiledUser>{


@InitBinder(value="registrationForm")
public void initBinder(WebDataBinder binder)
{
    binder.setValidator(new RegistrationFormValidator());
}
@控制器
@SessionAttributes(“userSearchForm”)
公共类UsersController扩展分页控制器{
@InitBinder(value=“registrationForm”)
公共绑定器(WebDataBinder绑定器)
{
binder.setValidator(新注册FormValidator());
}
我已经尝试了以下方法:

  • 删除注释并通过xml配置文件注入消息源
  • 实现MessageSourceAware接口
  • 尝试插入
    ReloadableresourceBundleMessageSource
    而不是使用接口
    MessageSource
  • 一切都以史诗般的失败告终;-) 如何正确注入MessageSource?

    我认为您正在检查CGLIB类的字段值

    更新有关自动布线的一般说明
    @Autowired
    注释由
    AutowiredNotationBeanPostProcessor
    处理,可通过在相应的spring配置文件中指定
    注释进行注册(bean后处理器基于每个容器工作,因此您需要为servlet和应用程序根上下文设置不同的后处理器=>您需要将
    放在web应用程序上下文和dispatcher servlet配置中)

    请注意,
    @Autowired
    注释具有
    required
    属性,该属性被设置为默认值true,这意味着如果自动连接过程发生,Spring将检查指定bean的一个实例是否存在。如果使用
    @Autowired
    注释字段的bean是一个单例,则将进行检查将在应用程序初始化期间执行


    更新在这个特定问题中,
    验证程序
    实例根本不是由Spring创建的,这就是为什么没有执行自动连线,也没有引发初始化异常。

    将basename更改为类似以下内容:

    <property name="basename" value="classpath:messages/messages" />
    

    当我试图在自定义日期格式化程序中使用MessageSource时,我遇到了类似的问题

    代码AppDateFormatter

    public class AppDateFormatter implements Formatter<Date> {
    
        @Autowired
        private MessageSource messageSource;
    
        other stuff.....
    
        private SimpleDateFormat createDateFormat(final Locale locale) {
            final String format = this.messageSource.getMessage("date.format", null, locale);
            final SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            dateFormat.setLenient(false);
           return dateFormat;
       }
    
    }
    

    在JavaConfigs类中添加以下bean

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
    
        return messageSource;
    }
    
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver resolver = new SessionLocaleResolver();
        resolver.setDefaultLocale(Locale.ENGLISH);
    
        return resolver;
    }
    
    @Bean
    public MessageSourceAccessor messageSourceAccessor(MessageSource messageSource){
        return new MessageSourceAccessor(messageSource, Locale.ENGLISH );
    }
    
    然后可以按如下方式注入
    MessageSourceAccessor
    bean

    @Autowired
    private MessageSourceAccessor msgs;
    
    获取消息字符串,如下所示

    msgs.getMessage("controller.admin.save.success")
    
    message\u en.properties
    文件应位于
    /src/main/resources
    文件夹中。还应为其他语言添加必要的属性文件


    希望这有帮助。

    可能相关:在其他spring上下文中有其他
    组件扫描吗?@Ralph-是的,我有。我在一个单独的上下文文件中有两个组件扫描,但据我所知,它们并不冲突。你怎么知道它真的是空的?@Autowired要求填充字段-spring打印异常吗初始化期间的堆栈跟踪,包括
    org.springframework.beans.factory.beancreatitionException:类的自动连线依赖项注入失败
    ?如何使用
    new
    操作符创建验证器实例?@Boris Treukhov-否,不会引发异常。调试时,我可以看到类RegistrationFormValidator的属性是空的。我认为注入出错了,因为我可以在其他地方使用servlet.xml文件中声明的messageSource。例如,在以不同形式解析错误消息时。我知道spring会自动执行此操作,所以我无法确定courtains背后的原理。
    @Autowired
    private MessageSourceAccessor msgs;
    
    msgs.getMessage("controller.admin.save.success")