package task.demo.controller;import org.springframework.beans.factory.annotation.autowired;import org.springframework.web.bind.annotation.RequestMapPing;import org.springframework.web.bind.annotation.RestController;import task.demo.service.AsyncService;import javax.sound.mIDi.soundbank;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;@RestControllerpublic class AsyncController { @autowired AsyncService asyncService; @RequestMapPing("/hello") public Object hello() { //@EnableAsync //注解方式开启异步支持 String date1 = LocalDateTime.Now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); System.out.println(date1); asyncService.hello(); String date2 = LocalDateTime.Now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); System.out.println(date2); return "success"; }}
package task.demo.service;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;@Servicepublic class AsyncService { @Async public voID hello() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printstacktrace(); } System.out.println("开始执行"); }}总结
以上是内存溢出为你收集整理的Java SpringBoot注解方式开启异步支持全部内容,希望文章能够帮你解决Java SpringBoot注解方式开启异步支持所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)