在Spring Boot中,可以使用多种方式来设置自动执行方法。
import org.springframework.scheduling.annotation.Scheduled;
@Scheduled(fixedRate = 5000) // 每隔5秒执行一次
public void doSomething() {
// 执行的代码逻辑
}
import javax.annotation.PostConstruct;
@PostConstruct
public void init() {
// 执行的代码逻辑
}
import org.springframework.beans.factory.InitializingBean;
public class MyBean implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
// 执行的代码逻辑
}
}
以上是一些常见的设置自动执行方法的方式,具体选择哪种方式取决于具体的需求和场景。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:springboot怎么执行ddl语句