posts - 431,  comments - 344,  trackbacks - 0

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

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

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

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

          事實上WebApplicationContextUtils.getWebApplicationContext方法就是使用上面的代碼實現的,建議使用上面上面的靜態方法 


          注意:在使用webApplicationContext.getBean("ServiceName")的時候,前面強制轉化要使用接口,如果使用實現類會報類型轉換錯誤。如:
          LUserService userService = (LUserService) webApplicationContext.getBean("userService");
          posted on 2008-01-15 16:42 周銳 閱讀(1894) 評論(1)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 秦皇岛市| 丹棱县| 鄄城县| 高邑县| 子洲县| 芒康县| 东兰县| 东丽区| 迭部县| 呼图壁县| 凤庆县| 陇川县| 清涧县| 英吉沙县| 武乡县| 黑山县| 静海县| 敦煌市| 张掖市| 元阳县| 东乡县| 莆田市| 景宁| 大关县| 宁阳县| 朝阳市| 仁寿县| 池州市| 沙湾县| 平原县| 大英县| 绵阳市| 五寨县| 桓台县| 炎陵县| 葵青区| 沙河市| 长兴县| 济宁市| 龙陵县| 南雄市|