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

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

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

    cuiyi's blog(崔毅 crazycy)

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

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

    需求:在系統(tǒng)啟動的時候把數(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é)果:程序啟動的時候會拋出NullPointException,因為datasource并沒有初始化好。

    摸索:Spring的事件機(jī)制:實現(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)程序中這個初始化被多次調(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拋出點

    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技術(shù)

    主站蜘蛛池模板: 国产亚洲美女精品久久久2020| 男女做羞羞的事视频免费观看无遮挡| 在线免费观看污网站| 亚洲福利视频网址| 18观看免费永久视频| 婷婷精品国产亚洲AV麻豆不片| 91成人免费福利网站在线| 亚洲av日韩av高潮潮喷无码| 日韩成人免费视频| 亚洲欧洲精品国产区| 歪歪漫画在线观看官网免费阅读| 亚洲国产乱码最新视频| 好爽又高潮了毛片免费下载| 欧美激情综合亚洲一二区| 国产无遮挡色视频免费视频| 一个人晚上在线观看的免费视频| 久久久久久A亚洲欧洲AV冫| 国产一区二区三区免费观在线| 亚洲av无码一区二区三区不卡| 67pao强力打造国产免费| 亚洲AV一二三区成人影片| 日韩高清在线免费观看| www免费插插视频| 亚洲国产一区二区三区青草影视 | 久久久久国色AV免费观看性色| 国产AV旡码专区亚洲AV苍井空| 在线a人片天堂免费观看高清| 久青草国产免费观看| 亚洲人成在线影院| 影音先锋在线免费观看| 羞羞视频免费网站日本| 久久亚洲AV午夜福利精品一区| 无码国产精品一区二区免费式影视 | 毛片免费在线观看网站| 国产亚洲综合久久| 亚洲国产精品无码AAA片| 国产99视频精品免费观看7| 日本免费精品一区二区三区| 亚洲天堂中文资源| 又粗又硬又黄又爽的免费视频 | 亚洲欧洲成人精品香蕉网|