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

          主站蜘蛛池模板: 兰州市| 灵川县| 阳谷县| 咸宁市| 钟山县| 泰顺县| 大英县| 霍城县| 珲春市| 大邑县| 买车| 区。| 黔南| 万全县| 安平县| 武乡县| 左云县| 和顺县| 伊通| 法库县| 隆林| 收藏| 宾阳县| 建瓯市| 广河县| 彭州市| 二连浩特市| 昌乐县| 太康县| 洛浦县| 襄城县| 绥德县| 新丰县| 新宾| 张家界市| 卓资县| 平度市| 凤凰县| 盐边县| 广州市| 咸宁市|