<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技術

    主站蜘蛛池模板: 久久精品国产亚洲AV电影| 亚洲精品国产电影| 亚洲精品色午夜无码专区日韩| 亚洲av无码兔费综合| 免费观看AV片在线播放| 亚洲欧洲精品久久| 国产乱子精品免费视观看片| 久久久久亚洲AV无码专区体验| 久久中文字幕免费视频| 亚洲欧洲第一a在线观看| 色猫咪免费人成网站在线观看| 亚洲一本综合久久| 97热久久免费频精品99| 亚洲av专区无码观看精品天堂| 成人黄页网站免费观看大全| 中文字幕乱码亚洲无线三区| 在线免费观看色片| 亚美影视免费在线观看| 国产亚洲精AA在线观看SEE| 永久免费AV无码网站国产| 国产亚洲精品a在线无码| 91av视频免费在线观看| 亚洲av午夜精品无码专区| 国产精品免费看久久久久| 男人和女人高潮免费网站| 亚洲无人区午夜福利码高清完整版 | 国产又黄又爽胸又大免费视频| 亚洲中文字幕无码久久精品1| 爱丫爱丫影院在线观看免费| 亚洲国产精品白丝在线观看| 免费看又爽又黄禁片视频1000| 日本中文字幕免费看| 亚洲AV无码国产精品色午友在线 | 亚洲AV综合色区无码一区爱AV| 日韩精品内射视频免费观看 | 一级毛片高清免费播放| 久久精品国产亚洲av麻豆色欲 | 国产成人精品免费视频大| 在线观看亚洲免费| 亚洲国产婷婷六月丁香| 蜜桃视频在线观看免费网址入口|