引入maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-task</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>html
Application 啓動類上加上@EnableTask註解spring
package xyz.zinglizingli;mybatis
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.scheduling.annotation.EnableScheduling;app
@EnableTask
@SpringBootApplication
@EnableScheduling
@MapperScan({"xyz.zinglizingli.*.mapper"})
public class CrawlApplication {maven
public static void main(String[] args) {
SpringApplication.run(CrawlApplication.class,args);
}
}ide
建立task類方式
實現CommandLineRunner 接口
經過註解的方式注入bean
@Component
public class HelloWorldCommandLineRunner implements CommandLineRunner {this
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
System.out.println("this is a task1");
}htm
}
經過@bean註解注入
@Bean
public CommandLineRunner commandLineRunner() {
return new HelloWorldCommandLineRunner();
}
接口
官方地址:get
https://docs.spring.io/spring-cloud-task/docs/2.0.0.RELEASE/reference/htmlsingle/#getting-started