学习Cron
pom.xml 添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
启动类添加 @EnableScheduling
@SpringBootApplication
@EnableScheduling //启动定时功能
@EnableWebSocket
@ComponentScan("com.company.*")
public class MainServerApplication {
public static void main(String[] args) {
SpringApplication.run(MainServerApplication.class, args);
}
}
编写定时类
@Component
public class Test1 {
@Autowired
private UserService userService;
private int i = 1;
@Scheduled(cron = "* * * * * ? ")
public void test1() {
System.out.println("-----"+(i++)+"-----");
}
}