Application Context定義
簡單來說就是Spring中的高級容器,可以獲取容器中的各種bean組件,註冊監聽事件,加載資源文件等功能。
具體定義可以參考官網:https://spring.io/understanding/application-context
Application Context獲取的幾種方式
1、直接注入
@Resource
private ApplicationContext ctx;
2、實現ApplicationContextAware接口(推薦)
3、WebApplicationContextUtils工具類
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
4、從當前線程綁定獲取(Spring boot不支持)
ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();