風人園

          弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
          隨筆 - 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

          主站蜘蛛池模板: 龙海市| 兖州市| 叶城县| 和林格尔县| 仙居县| 精河县| 襄城县| 江油市| 额尔古纳市| 扬州市| 吉安县| 会昌县| 台山市| 色达县| 雷波县| 湘潭市| 佛冈县| 资兴市| 绥化市| 中超| 封开县| 竹山县| 高尔夫| 天柱县| 武义县| 徐水县| 华坪县| 阿巴嘎旗| 大方县| 阿城市| 资中县| 西乌| 通河县| 余干县| 自贡市| 永登县| 棋牌| 玉树县| 南宫市| 新乐市| 吉木萨尔县|