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

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

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

    cuiyi's blog(崔毅 crazycy)

    記錄點(diǎn)滴 鑒往事之得失 以資于發(fā)展
    數(shù)據(jù)加載中……

    Spring-系統(tǒng)啟動時(shí)初始化數(shù)據(jù)庫的數(shù)據(jù)/系統(tǒng)啟動時(shí)操作數(shù)據(jù)庫

    需求:在系統(tǒng)啟動的時(shí)候把數(shù)據(jù)庫的數(shù)據(jù)進(jìn)行重置(或者插入數(shù)據(jù),或者取出一系列的數(shù)據(jù)緩存起來)

    摸索: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;
        }
    }

    結(jié)果:程序啟動的時(shí)候會拋出NullPointException,因?yàn)?span style="font-size: 13px; background-color: #eeeeee;">datasource并沒有初始化好。

    摸索:Spring的事件機(jī)制:實(shí)現(xiàn)ApplicationListener,在onApplicationEvent的方法進(jìn)行數(shù)據(jù)初始化操作,只要容器啟動,就會執(zhí)行這里的代碼。

    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
    }

    成功。

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

    怎么辦呢?
    好吧,既然是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拋出點(diǎn)

    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 閱讀(5784) 評論(0)  編輯  收藏 所屬分類: JavaEE技術(shù)

    主站蜘蛛池模板: 亚洲日韩乱码中文字幕| 大陆一级毛片免费视频观看| 哒哒哒免费视频观看在线www| 亚洲第一网站免费视频| 久久99精品免费视频| 亚洲成亚洲乱码一二三四区软件| 乱人伦中文视频在线观看免费| 国产精品成人四虎免费视频| 亚洲av成人中文无码专区| 永久黄网站色视频免费直播| 日韩色视频一区二区三区亚洲 | 国产亚洲欧美在线观看| 日韩免费毛片视频| 白白色免费在线视频| 亚洲国产成人久久笫一页| 免费大片av手机看片| 77777亚洲午夜久久多人| 国产精品免费一区二区三区| 亚洲精品无码鲁网中文电影| 久久久久国产精品免费免费不卡| 亚洲午夜免费视频| 成人性生交大片免费看无遮挡 | 永久免费av无码网站yy| 亚洲激情在线视频| 国产免费看JIZZ视频| 亚洲AV噜噜一区二区三区| 免费a级毛片视频| 免费无码作爱视频| 亚洲国产夜色在线观看| 国产成人精品高清免费| 一级毛片一级毛片免费毛片| 亚洲av日韩av激情亚洲| 成人网站免费大全日韩国产| 亚洲高清日韩精品第一区| 午夜神器成在线人成在线人免费| 免费的黄网站男人的天堂 | 亚洲一区二区免费视频| 美女被艹免费视频| 免费v片视频在线观看视频| 成人无码WWW免费视频| 亚洲综合色区中文字幕|