1

my service:

@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); 
   }
}

my controller:

@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 application class:

@SpringBootApplication
@EnableAsync
public class ApitestApplication {
   public static void main(String[] args) {
       SpringApplication.run(ApitestApplication.class, args);
   }
}

when i run my application, console log :

The bean 'forgetService' could not be injected as a 'com.apitest.service.ForgetService' because it is a JDK dynamic proxy that implements: com.apitest.inf.ForgetServiceInf

Action:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.


i tried setting 'spring.aop.proxy-target-class=true' in application.properties and setting '@EnableAsync(proxyTargetClass=true), but it's useless,so what's wrong? how to resolve it?

1
  • why you need @Async at two layers ? remove Async from controller or service. Commented Oct 25, 2018 at 4:41

1 Answer 1

1

please use below approach, it might help you to fix this issue.

@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());
}

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.