我的服务:
@Service
public class ForgetService{
@Async
public CompletableFuture<Object> getTokenService() {
Map<String, Object> map = new HashMap<>(8);
map.put("status", ErrorEnum.TOKEN_SUSSCESS.getStatus());
map.put("message", ErrorEnum.TOKEN_SUSSCESS.getMessage());
return CompletableFuture.completedFuture(map);
}
}
我的控制器:
@RestController
public class ForgetController {
private final ForgetService forgetService;
@Autowired
private ForgetController(ForgetService forgetService) {
this.forgetService = forgetService;
}
@PostMapping(value = "/forget/token")
@Async
public CompletableFuture<Object> getTokenController() {
return CompletableFuture.completedFuture(forgetService.getTokenService());
}
}
spring boot应用程序类:
@SpringBootApplication
@EnableAsync
public class ApitestApplication {
public static void main(String[] args) {
SpringApplication.run(ApitestApplication.class, args);
}
}
当我运行我的应用程序时,控制台日志:
Bean'forgetService'无法作为'com.apitest.service.ForgetService'注入,因为它是实现以下功能的JDK动态代理: com.apitest.inf.ForgetServiceInf
操作:
通过在@EnableAsync和/或@EnableCaching上设置proxyTargetClass = true,考虑将bean作为其接口之一注入或强制使用基于CGLib的代理。
我尝试在application.properties中设置'spring.aop.proxy-target-class = true'并设置'@EnableAsync(proxyTargetClass = true),但这没用,那怎么了?该如何解决?
答案 0 :(得分:1)
请使用以下方法,它可能有助于您解决此问题。
@Service
public class ForgetService{
@Bean("GetTokenService")
public CompletableFuture<Object> getTokenService() {
Map<String, Object> map = new HashMap<>(8);
map.put("status", ErrorEnum.TOKEN_SUSSCESS.getStatus());
map.put("message", ErrorEnum.TOKEN_SUSSCESS.getMessage());
return CompletableFuture.completedFuture(map);
}
@RestController
public class ForgetController {
private final ForgetService forgetService;
@Autowired
private ForgetController(ForgetService forgetService) {
this.forgetService = forgetService;
}
@PostMapping(value = "/forget/token")
@Async("GetTokenService")
public CompletableFuture<Object> getTokenController() {
return CompletableFuture.completedFuture(forgetService.getTokenService());
}
}