在使用springmvc时,我们也会在项目启动时初始化一些数据,具体的方式见下面的链接。
这里我只贴一下InitializingBean的例子。
注意事项:
-
springmvc和sping整合时,配置注解的注意事项!
不注意会导致我们的controller实例或其他的实例在初始化时加载两次!
springmvc
1
2
3
4
5
|
<!-- 指定一个包让其自动扫描:开启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
1
2
3
4
5
|
<!-- 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
>
|
下面是例子代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
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
其他线程了解
http://blog.51cto.com/hanchaohan/1888811
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
本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/2048404,如需转载请自行联系原作者