springmvc初始化數據
阿新 • • 發佈:2017-12-07
java springmvc initializingbean 在使用springmvc時,我們也會在項目啟動時初始化一些數據,具體的方式見下面的鏈接。
這裏我只貼一下InitializingBean的例子。
註意事項:
springmvc和sping整合時,配置註解的註意事項!
不註意會導致我們的controller實例或其他的實例在初始化時加載兩次!
springmvc
<!-- 指定一個包讓其自動掃描:開啟controller註解支持 --> <!-- 註意:如果base-package=com.book則註解事務會不起作用! --> <context:component-scan base-package="com.book.admin.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
spring
<!-- Scans for @Repository, @Service and @Component --> <!-- 註意:此處不排除controller的註解,會導致controller會初始化兩次! --> <context:component-scan base-package="com.book.*" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
下面是例子代碼:
package com.book.admin.controller; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSONObject; @Controller @RequestMapping("/testinit") public class TestInitController implements InitializingBean{ private static SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private ConcurrentHashMap<String, JSONObject> cacheData = new ConcurrentHashMap<String, JSONObject>(); private static final ScheduledExecutorService EXEC_TEST1 = Executors.newScheduledThreadPool(1); /** scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnitunit) 創建並執行一個在給定初始延遲後首次啟用的定期操作,後續操作具有給定的周期; 也就是將在 initialDelay 後開始執行, 然後在initialDelay+period 後執行, 接著在 initialDelay + 2 * period 後執行,依此類推 不管任務執行耗時是否大於間隔時間,scheduleAtFixedRate和scheduleWithFixedDelay都不會導致同一個任務並發地被執行。 唯一不同的是scheduleWithFixedDelay是當前一個任務結束的時刻,開始結算間隔時間, 如0秒開始執行第一次任務,任務耗時5秒,任務間隔時間3秒,那麽第二次任務執行的時間是在第8秒開始。 */ public TestInitController() { System.out.println("----------------------- init !!"); } @Override public void afterPropertiesSet() throws Exception { System.out.println(" ====== test InitializingBean !" + format.format(new Date())); /** * 創建並執行一個在給定初始延遲後首次啟用的定期操作,後續操作具有給定的周期;<br/> * 也就是將在 initialDelay 後開始執行,然後在initialDelay+period 後執行,<br/> * 接著在 initialDelay + 2 * period 後執行,依此類推。 * * 如果程序時間大於間隔時間,那麽每次執行完後,立即執行下一次! */ /* EXEC_TEST1.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("scheduleAtFixedRate ====== 可以調用一些方法初始化一些數據!begin:" + format.format(new Date())); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("scheduleAtFixedRate ====== 可以調用一些方法初始化一些數據!end:" + format.format(new Date())); System.err.println(""); } }, 10, 5000, TimeUnit.MILLISECONDS); */ /** * 創建並執行一個在給定初始延遲後首次啟用的定期操作,隨後, * 在每一次執行終止和下一次執行開始之間都存在給定的延遲。 * * scheduleWithFixedDelay是當前一個任務結束的時刻,開始結算間隔時間, 如0秒開始執行第一次任務,任務耗時5秒,任務間隔時間3秒,那麽第二次任務執行的時間是在第8秒開始。 */ /* EXEC_TEST1.scheduleWithFixedDelay(new Runnable() { @Override public void run() { System.out.println("scheduleWithFixedDelay ====== 可以調用一些方法初始化一些數據!begin:" + format.format(new Date())); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("scheduleWithFixedDelay ====== 可以調用一些方法初始化一些數據!end:" + format.format(new Date())); System.err.println(""); } }, 10, 5000, TimeUnit.MILLISECONDS)*/; /** * 創建並執行在給定延遲後的一次性操作 */ EXEC_TEST1.schedule(new Runnable() { @Override public void run() { System.out.println("---------- test ----------"); initData(); } }, 5000, TimeUnit.MILLISECONDS); } @RequestMapping("/test") @ResponseBody public String test() { return "JSON!"; } private void initData() { System.out.println(" ===== 可以把數據輸入緩存 cacheData 中!"); } }
參考文檔-裏面寫的更詳細:
java的Timer定時任務
http://blog.51cto.com/hanchaohan/1329591
spring中InitializingBean接口使用理解
http://www.pinhuba.com/spring/101053.htm
http://blog.51cto.com/ketqi/687681
http://blog.csdn.net/tsyj810883979/article/details/8481621
Timer的缺陷 用ScheduledExecutorService替代
http://blog.csdn.net/lmj623565791/article/details/27109467
springmvc的controller被初始化兩次
http://jinnianshilongnian.iteye.com/blog/1423971
https://www.cnblogs.com/haoke/p/4604883.html
http://jinnianshilongnian.iteye.com/blog/1762632
springmvc的簡單了解
http://blog.csdn.net/sunitjy/article/details/6782431
springmvc初始化數據