2013-04-09 32 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