spring 註解@Async的使用

  1. 開啓異步
@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;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章