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

    主站蜘蛛池模板: 中文字幕无码亚洲欧洲日韩| 日本亚洲视频在线| 亚洲宅男精品一区在线观看| 30岁的女人韩剧免费观看| 国产亚洲av片在线观看18女人| 中美日韩在线网免费毛片视频| 亚洲AⅤ视频一区二区三区 | 亚洲成人动漫在线观看| 久久成人无码国产免费播放| 久久精品国产精品亚洲毛片| 又粗又大又黑又长的免费视频| 亚洲大香人伊一本线| 韩国免费一级成人毛片| 日本亚洲免费无线码| 国产高清在线免费| 手机永久免费的AV在线电影网| 国产亚洲精品国看不卡| 最近免费mv在线观看动漫| 亚洲精品在线观看视频| 亚洲啪啪免费视频| 在线亚洲高清揄拍自拍一品区| 日韩免费无砖专区2020狼| 黄色一级视频免费观看| 亚洲中文字幕在线观看| 一级毛片在线免费观看| 亚洲国产成人超福利久久精品| 天天看免费高清影视| 黄色a三级免费看| 亚洲AV综合色区无码一区| 91高清免费国产自产| 亚洲熟妇自偷自拍另欧美| 亚洲AV永久无码精品一区二区国产 | 国产精品久免费的黄网站| 国产成人无码免费网站| 亚洲视频中文字幕| 最新中文字幕电影免费观看| a级毛片免费观看在线| 91亚洲国产成人精品下载| 永久免费毛片手机版在线看| A级毛片高清免费视频在线播放| 亚洲va乱码一区二区三区|