<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    cuiyi's blog(崔毅 crazycy)

    記錄點滴 鑒往事之得失 以資于發展
    數據加載中……

    Spring-系統啟動時初始化數據庫的數據/系統啟動時操作數據庫

    需求:在系統啟動的時候把數據庫的數據進行重置(或者插入數據,或者取出一系列的數據緩存起來)

    摸索:SBean可以IoC注入需要的資源比如DataSource;

    Spring Bean Config
    <bean id="idPoolsInitilizedProcessor" class="utils.IDPoolsInitilizedListener" scope="singleton" >
            <property name="datasource" ref="dataDS"/>
     </bean>

    Spring Bean Code
    public class JcIDPoolsInitilizedListener {
        private DataSource datasource = null
      
      public JcIDPoolsInitilizedBean() {
        System.out.println("%%%%%%%%%%%%%%");    
        try {
          //initilize msgid
          String refName = CxcConstants.REFCOUNTER_MSGID; 
          String sql = "update refcounter set nextnumber=(select max(msgid)+1 from msg) where refcounterid=?";
          update(sql, new Object[]{refName}); 
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }
        private int update(String anSql, Object[] args) throws Exception {
          int affactRows = 0;
          Connection con = null;
          PreparedStatement stmt = null;
          try {
            con = datasource.getConnection();
            stmt = con.prepareStatement(anSql);
            setSQLParams(stmt, args);
            affactRows = stmt.executeUpdate();
            return affactRows;
          } finally {
            try
              if (null != stmt) {
                stmt.close();
              }
              if (null != con) {
                con.close();
              }
            } catch (Exception ex) {
              ex.printStackTrace();
            }
          }
        }
        
        private void setSQLParams(PreparedStatement stmt, Object[] args) throws Exception {
          if (null != args && 0 < args.length) {
            for (int i = 0, n = args.length; i < n; i++) {
              stmt.setObject(i + 1, args[i]);
            }
          }
        }

        public DataSource getDatasource() {
          return datasource;
        }

        public void setDatasource(DataSource datasource) {
          this.datasource = datasource;
        }
    }

    結果:程序啟動的時候會拋出NullPointException,因為datasource并沒有初始化好。

    摸索:Spring的事件機制:實現ApplicationListener,在onApplicationEvent的方法進行數據初始化操作,只要容器啟動,就會執行這里的代碼。

    public class JcIDPoolsInitilizedListener implements ApplicationListener {
      
      private DataSource datasource = null
      
      public void onApplicationEvent(ApplicationEvent argo) {
        //todo: code is same as previous 
      }
        
      //todo: all the other part is same as previous
    }

    成功。

    然后的然后呢?會發現程序中這個初始化被多次調用。
    為什么呢? 原因是Listener定義不到位。
    為什么呢? 只要是ApplicationEvent都會觸發,默認的事件是org.springframework.security.access.event.PublicInvocationEvent,肯定觸發的。

    怎么辦呢?
    好吧,既然是Listener,總得告訴它Listen什么Event吧。
    第一 定義Listener
    public class JcIDPoolsInitilizedListener implements ApplicationListener {
      
      private DataSource datasource = null
      
      public void onApplicationEvent(ApplicationEvent argo) {
         if (argo instanceof IDPoolsInitilizedEvent) {
            //todo: code is same as previous 
         }
       
         //todo: all the other part is same as previous
    }

    第二 定義Event
    public class IDPoolsInitilizedEvent extends ApplicationEvent{
      private static final long serialVersionUID = 646140097162842368L;
      
      public IDPoolsInitilizedEvent(Object source){
         super(source);
      }
    }

    第三 定義Event拋出點

    public class IDPoolsInitilizedBean implements ApplicationContextAware{
      private ApplicationContext applicationContext;
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        IDPoolsInitilizedEvent event = new IDPoolsInitilizedEvent("IDPoolsInitilized");
        this.applicationContext.publishEvent(event);
      }
    }

    第四 定義配置文件
    <bean id="idPoolsInitilizedListenerProcessor" class="utils.IDPoolsInitilizedListenerBean"
            scope
    ="singleton" >
            <property name="datasource" ref="dataDS"/>
        </bean>
        <bean id="idPoolsInitilizedProcessor" class="utils.IDPoolsInitilizedBean"
            scope
    ="singleton" />

    posted on 2013-03-17 19:55 crazycy 閱讀(5775) 評論(0)  編輯  收藏 所屬分類: JavaEE技術

    主站蜘蛛池模板: 一区二区三区亚洲| 亚洲精品美女久久久久9999| 亚洲成年人免费网站| 国产美女无遮挡免费视频| 亚洲精品人成电影网| 日韩午夜理论免费TV影院| 久久精品国产亚洲Aⅴ香蕉 | 亚洲一本大道无码av天堂| 麻豆狠色伊人亚洲综合网站| 67194成手机免费观看| 亚洲人成电影亚洲人成9999网| 伊人久久大香线蕉免费视频| 亚洲第一页综合图片自拍| 亚洲AV成人无码久久WWW| 成人男女网18免费视频| 亚洲精华国产精华精华液| 好男人视频社区精品免费| 亚洲精品理论电影在线观看| 无码一区二区三区免费视频| 久久久久久亚洲精品无码| 免费一级毛片在播放视频| 国产精品免费久久久久电影网| 亚洲中文字幕无码专区| 日本人成在线视频免费播放| 亚洲av福利无码无一区二区| 5555在线播放免费播放| 亚洲砖码砖专无区2023 | 老司机精品视频免费| 亚洲男人第一无码aⅴ网站| a级午夜毛片免费一区二区| 亚洲欧洲日韩在线电影| 国产精品色午夜免费视频| 一级毛片一级毛片免费毛片| 日木av无码专区亚洲av毛片| 丁香花在线观看免费观看| 免费看黄网站在线看 | 久久精品亚洲中文字幕无码麻豆 | 国产精品免费电影| 久久成人a毛片免费观看网站| 亚洲男人天堂2018av| 亚洲日韩精品无码一区二区三区|