0

如何从拦截器中提供的 MessageContext 获取 ServletContext 对象?下面的 TODO 假设是一个 ServletContext。

@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint)
        throws Exception {

    WebApplicationContext applicationContext =
            WebApplicationContextUtils.getWebApplicationContext(TODO);
            TestIdentitiesService service = applicationContext.getBean(TestIdentitiesService.class);
4

2 回答 2

2

您可以尝试以下方法:

@Autowired
private ServletContext context;

@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint)
        throws Exception {

    WebApplicationContext applicationContext =
            WebApplicationContextUtils.getWebApplicationContext(context);
    TestIdentitiesService service =
    applicationContext.getBean(TestIdentitiesService.class);
于 2013-04-11T11:52:36.727 回答
1

您应该能够获得具有以下内容的 ServletContext:

messageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT);
于 2013-04-10T08:08:46.947 回答