<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ù)

    主站蜘蛛池模板: 久久国产精品免费视频| 日韩a级毛片免费观看| 99久久久国产精品免费无卡顿| 妞干网在线免费观看| 亚洲综合另类小说色区| 亚洲人xxx日本人18| a级毛片在线免费看| 永久免费AV无码网站在线观看| 亚洲精品白浆高清久久久久久| 亚洲欧美日韩中文字幕一区二区三区 | 久久久久久久久免费看无码| 亚洲午夜久久久影院伊人| 亚洲国产AV一区二区三区四区| 日韩免费无码一区二区三区| 亚洲欧洲中文日韩av乱码| 一本色道久久88—综合亚洲精品| 永久免费av无码入口国语片| 日韩精品成人亚洲专区| 最新国产精品亚洲| 天天影院成人免费观看| 国产成A人亚洲精V品无码| 国产99久久亚洲综合精品| 好男人视频社区精品免费| 亚洲国产精华液2020| 亚洲国产精品一区二区九九| 亚洲人成77777在线播放网站不卡| 成人最新午夜免费视频| 亚洲中文无码a∨在线观看| 特级无码毛片免费视频| 国产在线a免费观看| 亚洲第一精品电影网| 毛片无码免费无码播放| 亚洲天然素人无码专区| 久久久久亚洲AV成人网人人软件| 亚洲日韩在线中文字幕综合| 国产精品免费综合一区视频| 亚洲一区精彩视频| 亚洲午夜成人精品电影在线观看| 成人嫩草影院免费观看| 亚洲国产精品碰碰| 亚洲精品国产免费|