- 开启异步
@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;
}