Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d78ae7a

Browse files
spring-测试
1 parent 1187b15 commit d78ae7a

6 files changed

Lines changed: 225 additions & 64 deletions

File tree

‎springBoot/.idea/workspace.xml‎

Lines changed: 100 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎springBoot/src/test/java/cn/hncu/p3/p7_fortest/DemoBeanIntegrationTests.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package cn.hncu.p3.p7_fortest;
22

3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.test.context.ActiveProfiles;
8+
import org.springframework.test.context.ContextConfiguration;
39
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
410

511
/**
@@ -9,5 +15,18 @@
915
* Time: 下午 9:27.
1016
* Explain:测试类---源码在src/test/java下
1117
*/
18+
@RunWith(SpringJUnit4ClassRunner.class)//在SpringJUnit4ClassRunner在Junit环境下提供Spring TextContext Framework的功能
19+
@ContextConfiguration(classes = {TestConfig.class})//@ContextConfiguration用来加载配置ApplicationContext,其中classes属性用来加载配置类
20+
@ActiveProfiles("prod")
1221
public class DemoBeanIntegrationTests {
22+
@Autowired//可以使用普通的@Autowired注入Bean
23+
private TestBean testBean;
24+
25+
@Test//测试代码,通过Junit的Assert来校验结果是否和预期的一样
26+
public void prodBeanShouldInject(){
27+
String expected = "production profile";
28+
String actual = testBean.getContent();
29+
System.out.println(actual);
30+
Assert.assertEquals(expected,actual);//用来查看对象中存的值是否是期待的值,与字符串比较中使用的equals()方法类似;
31+
}
1332
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
进行本示例的演示,需要先配置好Maven和Spring哦、
2+
见:
3+
<a href="http://blog.csdn.net/qq_26525215/article/details/53010442" target='_blank'>【Spring】基于IntelliJ IDEA搭建Maven</a>
4+
5+
#分析
6+
7+
要实现计划任务,首先通过在配置类注解@EnableScheduling来开启对计划任务的支持,然后在要执行计划任务的方法上注解@Scheduled,声明这是一个计划任务。
8+
9+
Spring通过@Scheduled支持多种类型的计划任务,包含cron、fixDelay、fixRate等。
10+
11+
在本示例中:
12+
使用cron属性可按照指定时间执行,本例写的是每天20点07分执行;
13+
14+
#示例
15+
16+
##计划任务执行类
17+
18+
在这个类中的方法上需要@Scheduled注解配合@EnableScheduling使用。
19+
20+
```
21+
package cn.hncu.p3.p3_taskscheduler;
22+
23+
import org.springframework.scheduling.annotation.Scheduled;
24+
import org.springframework.stereotype.Service;
25+
26+
import java.text.SimpleDateFormat;
27+
import java.util.Date;
28+
29+
/**
30+
* Created with IntelliJ IDEA.
31+
* User: 陈浩翔.
32+
* Date: 2016年11月22日.
33+
* Time: 下午 10:25.
34+
* Explain:计划任务执行类
35+
*/
36+
@Service
37+
public class ScheduledTaskService {
38+
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
39+
40+
@Scheduled(fixedRate = 5000) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行
41+
public void reportCurrentTime(){
42+
System.out.println("每隔5秒执行一次 "+dateFormat.format(new Date()));
43+
}
44+
45+
@Scheduled(cron = "0 07 20 ? * *" ) //使用cron属性可按照指定时间执行,本例指的是每天20点07分执行;
46+
//cron是UNIX和类UNIX(Linux)系统下的定时任务
47+
public void fixTimeExecution(){
48+
System.out.println("在指定时间 "+dateFormat.format(new Date())+" 执行");
49+
}
50+
}
51+
52+
```
53+
54+
##配置类
55+
56+
通过@EnableScheduling注解开启对计划任务的支持
57+
58+
```
59+
package cn.hncu.p3.p3_taskscheduler;
60+
61+
import org.springframework.context.annotation.ComponentScan;
62+
import org.springframework.context.annotation.Configuration;
63+
import org.springframework.scheduling.annotation.EnableScheduling;
64+
65+
/**
66+
* Created with IntelliJ IDEA.
67+
* User: 陈浩翔.
68+
* Date: 2016年11月22日.
69+
* Time: 下午 10:32.
70+
* Explain:配置类
71+
*/
72+
73+
@Configuration
74+
@ComponentScan("cn.hncu.p3.p3_taskscheduler")
75+
@EnableScheduling //通过@EnableScheduling注解开启对计划任务的支持
76+
public class TaskScheduleConfig {
77+
}
78+
79+
```
80+
81+
##运行类
82+
83+
```
84+
package cn.hncu.p3.p3_taskscheduler;
85+
86+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
87+
88+
/**
89+
* Created with IntelliJ IDEA.
90+
* User: 陈浩翔.
91+
* Date: 2016年11月22日.
92+
* Time: 下午 10:34.
93+
* Explain:运行类
94+
*/
95+
public class Main {
96+
public static void main(String[] args) {
97+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskScheduleConfig.class);
98+
}
99+
}
100+
101+
```
102+
103+
#运行结果
104+
105+
![](http://img.blog.csdn.net/20161209201152733)
106+
575 Bytes
Binary file not shown.
881 Bytes
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /