posts - 431,  comments - 344,  trackbacks - 0

          在servlet或者filter或者Listener中使用spring的IOC容器的方法是:

          WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); 

          由于spring是注入的對(duì)象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 對(duì)象:

          WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

          事實(shí)上WebApplicationContextUtils.getWebApplicationContext方法就是使用上面的代碼實(shí)現(xiàn)的,建議使用上面上面的靜態(tài)方法 


          注意:在使用webApplicationContext.getBean("ServiceName")的時(shí)候,前面強(qiáng)制轉(zhuǎn)化要使用接口,如果使用實(shí)現(xiàn)類會(huì)報(bào)類型轉(zhuǎn)換錯(cuò)誤。如:
          LUserService userService = (LUserService) webApplicationContext.getBean("userService");
          posted on 2008-01-15 16:42 周銳 閱讀(1895) 評(píng)論(1)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 南汇区| 澄江县| 于都县| 水富县| 信阳市| 赤城县| 周口市| 龙岩市| 泾川县| 安宁市| 社会| 九台市| 万年县| 岳池县| 苍溪县| 雅安市| 太康县| 黄梅县| 易门县| 色达县| 古丈县| 剑阁县| 聊城市| 交口县| 锡林郭勒盟| 玉山县| 万全县| 雅安市| 铁力市| 阿克苏市| 隆安县| 遂昌县| 永善县| 鲜城| 湖南省| 聂拉木县| 庆元县| 常德市| 酒泉市| 晋城| 和林格尔县|