Spring Boot學習筆記(三)

2022-07-09 17:51:14 字數 979 閱讀 5603

在我們開發專案過程中,經常需要定時任務來幫助我們來做一些內容, spring boot 預設已經幫我們實行了,只需要新增相應的註解就可以實現

pom 包裡面只需要引入 spring boot starter 包即可

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

在啟動類上面加上@enablescheduling即可開啟定時 

@enablescheduling

public

class

public

static

void

main(string args)

}定時任務1:

@component

public

class

schedulertask

}

定時任務2:

@component

public

class

scheduler2task

}

結果如下:

this is scheduler task runing 0

this is scheduler task runing 1

this is scheduler task runing 2

this is scheduler task runing 3

@scheduled引數可以接受兩種定時的設定,一種是我們常用的cron="*/6 * * * * ?",一種是fixedrate = 6000,兩種都表示每隔六秒列印一下內容。

fixedrate 說明

Spring boot 學習筆記(三)

一.bean的scope scope描述的是spring容器如何新建bean的市裡的。spring的scope有以下幾種,通過 scope註解來實現。1 singleton 乙個spring容器中只有乙個bean的例項,為此spring的預設配置,全容器 共享乙個例項。2 prototype 每次呼...

SpringBoot學習筆記(三)

restcontroller 註解,對具有返回型別的controller類使用。相當於 responsebody controller。使用 restcontroller的目的在於使用傳統方法是返回資料格式都為json,需要使用 responsebody註解,但是 restcontroller修飾的...

springboot 學習筆記(三)

在實現開發中配置檔案會有多個 可以將主配置調換到不同的配置檔案中 以下配置改變工作區配置檔案 啟用某個配置檔案 spring.profiles.active demo 注意在專案檔案下編寫乙個控制器 controller public class democontroller private str...