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;
    }
发布了192 篇原创文章 · 获赞 45 · 访问量 33万+

猜你喜欢

转载自blog.csdn.net/flymoringbird/article/details/93921399