風人園

          弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
          隨筆 - 99, 文章 - 181, 評論 - 56, 引用 - 0
          數據加載中……

          ECApp -- 之系統初始化

          在java web項目中,最常用的就是使用listener來實現初始化了。

          下面是實現代碼
           1 public class StartupListener extends ContextLoaderListener implements
           2         ServletContextListener {
           3 
           4     private Logger logger = LoggerFactory.getLogger(getClass());
           5     
           6     /**
           7      * web容器啟動時調用
           8      */
           9     public void contextInitialized(ServletContextEvent event) {
          10         logger.info("application servlet initialized");
          11 
          12         ServletContext context = event.getServletContext();
          13         
          14         //從spring中獲得bean,由這個bean進行相應操作,結果存到context中
          15         ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
          16         ProductClassManager productClassManager = (ProductClassManager)ctx.getBean("productClassManager");
          17         
          18         String prodClassTreeJson = productClassManager.generateProdClassTree();
          19         
          20         context.setAttribute(Constants.PRODUCT_CLASS_JSON_STRING, prodClassTreeJson);
          21     }
          22     
          23     
          24     /**
          25      * web容器銷毀時調用
          26      */
          27     public void contextDestroyed(ServletContextEvent event){
          28         logger.info("application servlet destroyed");
          29     }
          30 
          31 }
                  這里面還調用 了spring的bean,所以listener要配置在spring的那個listener下面,否則可能出錯。

          這樣我們就可以在系統啟動的時候將一些常用的數據及配置信息都放到內存(即serlvetContext里面),在struts2中,我們可以通過這種方式獲得
          ServletContext context = ServletActionContext.getServletContext();
          然后就可以使用存放在context里面的數據了。
          就這么簡單。

          posted on 2009-08-06 15:40 風人園 閱讀(261) 評論(0)  編輯  收藏 所屬分類: ECP

          主站蜘蛛池模板: 阿克| 黄骅市| 象山县| 韶关市| 会宁县| 汪清县| 兴化市| 十堰市| 广汉市| 韩城市| 扎赉特旗| 房山区| 东丽区| 洪洞县| 图们市| 固镇县| 广饶县| 齐河县| 镇安县| 自贡市| 蒙阴县| 行唐县| 临沧市| 宁国市| 叶城县| 修文县| 汝城县| 文登市| 乌拉特中旗| 赤水市| 桓仁| 宁安市| 分宜县| 兰州市| 玉龙| 浮梁县| 青田县| 洛宁县| 博湖县| 麻栗坡县| 沽源县|