2013-04-09 94 views
0

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

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

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

回答

1

你可以尝试以下方法:

@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); 
+0

这个怪事工作得很好。 – 2013-04-11 20:20:35

1

你应该能够得到一个ServletContext有以下几点:

messageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT); 
+0

性能上的MessageContext对象是空的,所以它不工作。 – 2013-04-10 18:28:22