在使用springmvc时,咱们也会在项目启动时初始化一些数据,具体的方式见下面的连接。html
这里我只贴一下InitializingBean的例子。java
注意事项:web
springmvc和sping整合时,配置注解的注意事项!spring
不注意会致使咱们的controller实例或其余的实例在初始化时加载两次!express
springmvcjson
<!-- 指定一个包让其自动扫描:开启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 中!"); } }
参考文档-里面写的更详细:mvc
java的Timer定时任务app
http://www.javashuo.com/article/p-ujktjzvm-gr.html
其余线程了解
http://www.javashuo.com/article/p-kfuecxrq-gv.html
spring中InitializingBean接口使用理解
http://www.pinhuba.com/spring/101053.htm
http://www.javashuo.com/article/p-getflddl-gz.html
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
http://www.javashuo.com/article/p-pridpmyv-dh.html
http://jinnianshilongnian.iteye.com/blog/1762632
springmvc的简单了解