基於Spring註解方式配置Quartz
之前我們都是通過基於XML的方式實現Spring Quartz 雖然配置起來特別的方便,但是Spring還支援基本註解的方式來配置,這樣做不僅更加簡單,而且程式碼量也更加少了很多。
1、配置需要排程的類,並添加註解
import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class HelloJob { public HelloJob() { System.out.println("HelloJob建立成功"); } @Scheduled(cron = "0/1 * * * * ? ") // 每隔1秒隔行一次 public void run() { System.out.println("Hello MyJob " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date())); } }
2、首先要配置我們的beans.xml,在xmlns 多加下面的內容 xmlns:task="http://www.springframework.org/schema/task"
3、然後xsi:schemaLocation多加下面的內容 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
4、自動配置掃描spring配置檔案裡面配置內容 <!--開啟這個配置,spring才能識別@Scheduled註解--> <task:annotation-driven/> <!-- 自動掃描註解的bean --> <context:component-scan base-package="com.binnor"/>
原文:https://blog.csdn.net/tanyongbing1988/article/details/45689987