風人園

          弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
          隨筆 - 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 風人園 閱讀(254) 評論(0)  編輯  收藏 所屬分類: ECP

          主站蜘蛛池模板: 衡南县| 陕西省| 长顺县| 工布江达县| 滨海县| 太湖县| 鱼台县| 永春县| 洪江市| 三明市| 静安区| 甘肃省| 汶川县| 石家庄市| 吉木萨尔县| 大埔县| 义马市| 银川市| 林西县| 安多县| 扎鲁特旗| 万宁市| 贵德县| 襄垣县| 丹阳市| 怀集县| 渝中区| 策勒县| 佳木斯市| 上栗县| 余姚市| 荆州市| 沁阳市| 宜州市| 信阳市| 安徽省| 大丰市| 观塘区| 苍梧县| 阜阳市| 云林县|