码上敲享录 > Spring框架常见问题详解分享 > 获取spring上下文ApplicationContext

获取spring上下文ApplicationContext

上一章章节目录下一章 2018-06-03已有476人阅读 评论(0)

java如何获取spring上下文ApplicationContext?

解决方法:

public static ApplicationContext getRequiredWebApplicationContext() {
   ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes()
;
   if (attributes == null) {
throw new RuntimeException("非web请求,非法调用!");
} else {
       HttpServletRequest request = attributes.getRequest()
;
ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
       return ac;
}
}

本文地址:http://www.yayihouse.com/yayishuwu/chapter/1281

0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交