>  기사  >  Java  >  Springboot의 비동기 작업 분석 예

Springboot의 비동기 작업 분석 예

WBOY
WBOY앞으로
2023-05-10 23:31:041106검색

비동기 작업

시작 클래스

@MapperScan("com.topcheer.*.*.dao")
@SpringBootApplication
@EnableCaching
@EnableRabbit
@EnableAsync
public class Oss6Application {
  public static void main(String[] args) {
    SpringApplication.run(Oss6Application.class, args);
  }
}

컨트롤러 레이어

/**
 * @author WGR
 * @create 2019/10/12 -- 21:53
 */
@RestController
public class AsynController {

  @Autowired
  AsynService asyncService;

  @GetMapping("/hello")
  public String hello(){
    asyncService.hello();
    return "success";
  }
}

서비스 레이어

/**
 * @author WGR
 * @create 2019/10/12 -- 21:52
 */
@Service
public class AsynService {

  //告诉Spring这是一个异步方法
  @Async
  public void hello() {
    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("处理数据中...");
  }

}

테스트 결과:

페이지에 직접 성공이 표시되고 콘솔에 3초 후에 처리 데이터가 표시됩니다...

위 내용은 Springboot의 비동기 작업 분석 예의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제