21.异步任务

1.启动类加注解

@EnableAsync//开启异步任务
@SpringBootApplication
public class DemoApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(DemoApplication.class, args);
    }

}

2.需要异步处理的方法加注解

@Service
public class AysSercive {
    
    
    @Async
    public void hello(){
    
    
        try {
    
    
            Thread.sleep(3000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println("数据处理中...");
    }
}

controller

@RestController
public class AysController {
    
    
    @Autowired
    private AysSercive aysSercive;

    @RequestMapping("/hello")
    public  String  getHeool(){
    
    
        aysSercive.hello();
        return "ok";
    }
}

猜你喜欢

转载自blog.csdn.net/zyf_fly66/article/details/113944322
21.