Spring 中获取servletContext及WebApplicationContext

2014-11-24 10:24:24 · 作者: · 浏览: 0

今天想在系统启动时,读数据库并初始化一些参数到application中,一下子还没弄出来,查了一下api才知道。spring中获取servletcontext 代码如下,关键是要知道 contextLoader的强大!其它很简单


WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();   
        ServletContext servletContext = webApplicationContext.getServletContext(); 

    WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); 
            ServletContext servletContext = webApplicationContext.getServletContext();


spring 中获取applicationContext其实也很简单只要类实现ApplicationContextAware 接口即可,这样你就可获取上下文中所有bean,很好使的。