一、引言
在前边的文章《[springboot:使用异步注解@Async的那些坑》中介绍了使用@Async注解获取任务执行结果的错误用法,今天来分享下另外一种常见的错误。
二、代码演示
下面是我的controller的代码,
package com.atssg.controller;
import com.atssg.service.MyAsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RequestMapping("/sync/")
@RestController
public class SyncController2 {
@Autowired
private MyAsyncService syncService;
@GetMapping("/test2")
public String test2() {
return syncService.syncMethod("hello world");
}
}
在controller中调用了service层的syncMethod方法,下面看该方法的定义,
package com.atssg.service;
import lombok.extern.slf4