如何从拦截器提供的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);

最佳答案

您可以尝试以下方法:

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

10-05 23:20