天天看點

Spring 中擷取servletContext及WebApplicationContext以及applicationContext三者之間的關系

spring中擷取servletcontext 代碼如下,關鍵是要知道 contextloader的強大!其它很簡單

webapplicationcontext webapplicationcontext = contextloader.getcurrentwebapplicationcontext();

servletcontext servletcontext = webapplicationcontext.getservletcontext();

spring 中擷取applicationcontext其實也很簡單隻要類實作applicationcontextaware 接口即可,這樣你就可擷取上下文中所有bean,很好使的。