- 開啓異步
@SpringBootApplication
@EnableAsync //開啓異步
public class RenrenApplication {
public static void main(String[] args) {
SpringApplication.run(RenrenApplication.class, args);
}
}
或者再config類上開啓
@Configuration @ComponentScan(value = "com.learn") //掃描註冊到容器的類 @EnableAsync public class Config { }
2. 註解異步方法
@Transactional
@Async
public void save(Long id){}
3.使用異步
public MRecordEntity getById(Serializable id) {
MRecordEntity record = super.getById(id);
if (record != null) {
viewedService.save(record.getId()); //此方法是異步方法
}
return record;
}