【问题标题】:MessageSource bean injected null in custom exception mapper classMessageSource bean 在自定义异常映射器类中注入 null
【发布时间】:2015-08-10 08:11:20
【问题描述】:

我试图理解为什么 messageSource bean 在下面的代码中被注入 null :

@Provider
public class BadRequestExceptionMapper implements ExceptionMapper<BadRequestException>{
@Autowired
MessageSource messageSource;


@Override
public Response toResponse(BadRequestException ex) {
    String message = null;
    try{
    message = messageSource.getMessage(ex.getErrorCode().getErrorKey(),null,null);

    }catch(Exception e){
    e.printStackTrace();
    }
    return Response.status(400).entity(message).type("text/plain")
            .build();
   }

}

下面是我的 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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:component-scan base-package="com.infonexis" />
<context:annotation-config />

<import resource="applicationPersistence.xml" />
<import resource="applicationService.xml" />
<import resource="applicationContext-datasource.xml" />

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename">
<value>MessageResources_en_US</value>
</property>
<property name="cacheSeconds" value="1"/>
</bean>

</beans>

我正在尝试在自定义异常映射器类中自动装配 MessageSource bean,但它被注入 null,以前当我从 ClassPathXmlApplicationContext 加载上下文时它正在工作,如下所示:

@Provider
public class BadRequestExceptionMapper implements ExceptionMapper<BadRequestException>{

@Override
public Response toResponse(BadRequestException ex) {
    String message = null;
    try{
    ApplicationContext context = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
    MessageSource messageSource = 
     (MessageSource)context.getBean("messageSource");

    message = messageSource.getMessage(ex.getErrorCode().getErrorKey(),null,null);

    }catch(Exception e){
    e.printStackTrace();
    }
    return Response.status(400).entity(message).type("text/plain")
            .build();
  }

}

我尝试了以下方法

  1. 实现org.springframework.context.MessageSourceAware接口

  2. 在 BadRequestExceptionMapper 类中添加@Component 注解

  3. 尝试注入 ReloadableresourceBundleMessageSource 而不是使用接口 MessageSource

但一切都失败了。

上下文组件扫描设置正确。 如何正确注入 MessageSource?

【问题讨论】:

    标签: java spring dependency-injection


    【解决方案1】:

    我认为您的@Provider 在您的 web.xml 中定义了一些上下文侦听器,可能在 spring 应用程序上下文侦听器之前启动。

    尝试将 Spring 侦听器 org.springframework.web.context.ContextLoaderListener 放在 Web 服务侦听器之前,例如 com.sun.xml.ws.transport.http.servlet.WSServletContextListener(或)您的任何 JAX-RS servlet像球衣一样的听众。

    【讨论】:

    • 是的,我已经添加了一个弹簧监听器org.springframework.web.context.ContextLoaderListener。我以某种方式设法解决了这个问题。我注入 AppicationContext 并使用它的 setter 方法自动装配它。这从上下文中正确注入了 messageSource bean,但没有从属性文件中提取消息我收到错误消息 “找不到代码 **** 和语言环境 ** 的消息” 所以我改变了从“ReloadableResourceBundleMessageSource”到“ResourceBundleMessageSource”的消息源及其现在工作。我不确定这是否是解决问题的理想方案。
    猜你喜欢
    • 2015-10-07
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多