有两种办法:
1. 传入servletContext获取
WebApplicationContext wac =WebApplicationContextUtils.getWebApplicationContext(servletContext);
2. 通过Thread.currentThread().getContextClassLoader()获取
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
以上两种方法一定要在web.xml 上添加ContextLoaderListener才能获取.
spring源码如下:
org.springframework.web.context.ContextLoader
扫描二维码关注公众号,回复:
549050 查看本文章