获取Spring容器的WebApplicationContext对象

有两种办法:

 

1. 传入servletContext获取

WebApplicationContext wac =WebApplicationContextUtils.getWebApplicationContext(servletContext);

2. 通过Thread.currentThread().getContextClassLoader()获取

WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();

 

以上两种方法一定要在web.xml 上添加ContextLoaderListener才能获取.

spring源码如下:

org.springframework.web.context.ContextLoader

获取Spring容器的WebApplicationContext对象_第1张图片

你可能感兴趣的:(获取Spring容器的WebApplicationContext对象)