英文:
@Async giving me error in spring boot application
问题
这是主应用程序类:
@EnableAsync(proxyTargetClass = true)
@EnableDiscoveryClient
@SpringBootApplication
public class ProductApplication {
public static void main(final String[] args) {
SpringApplication.run(ProductApplication.class, args);
}
@Bean("threadPoolTaskExecutor")
public TaskExecutor getAsyncExecutor() {
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(20);
executor.setMaxPoolSize(1000);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setThreadNamePrefix("Async-");
return executor;
}
}
这是服务类:
@Component
public class ProductServiceImpl implements ProductService {
@Autowired
ProductRepository productRepository;
@Autowired
private ProductHandler productHandler;
@Async
@Override
public List<String> getAllCategories() {
final List<String> finalList = new ArrayList<>();
return finalList;
}
}
这是控制器类:
@RestController
public class ProductResource {
@Autowired
private ProductServiceImpl productServiceImpl;
@GetMapping("/categories")
public ResponseEntity<List<String>> getAllCategories() {
return new ResponseEntity<>(this.productServiceImpl.getAllCategories(), HttpStatus.OK);
}
}
我已经在服务实现方法上使用了@Async
注解,但是我得到了这个错误:
> 操作:
>
> 考虑将该bean注入为其接口之一,或者通过在@EnableAsync和/或@EnableCaching上设置proxyTargetClass=true来强制使用基于CGLib的代理。
如果我尝试在控制器上加上注解,我的GET请求会得到空的响应。我已经尝试了包括将proxyTargetClass
设置为true在内的所有方法。
英文:
This is main application class:
@EnableAsync(proxyTargetClass = true)
@EnableDiscoveryClient
@SpringBootApplication
public class ProductApplication {
public static void main(final String[] args) {
SpringApplication.run(ProductApplication.class, args);
}
@Bean("threadPoolTaskExecutor")
public TaskExecutor getAsyncExecutor() {
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(20);
executor.setMaxPoolSize(1000);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setThreadNamePrefix("Async-");
return executor;
}
}
This is service class:
@Component
public class ProductServiceImpl implements ProductService {
@Autowired
ProductRepository productRepository;
@Autowired
private ProductHandler productHandler;
@Async
@Override
public List<String> getAllCategories() {
final List<String> finalList = new ArrayList<>();
return finalList;
}
}
This is the controller class:
@RestController
public class ProductResource {
@Autowired
private ProductServiceImpl productServiceImpl;
@GetMapping("/categories")
public ResponseEntity<List<String>> getAllCategories() {
return new ResponseEntity<>(this.productServiceImpl.getAllCategories(), HttpStatus.OK);
}
}
I have annotated the service implementation method with @Async
but I get this error:
> 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.
If I try annotating the controller I get empty response to my get request. I have try every thing including setting proxyTargetClass
to true.
答案1
得分: 3
只要您使用@Async
注解,就必须熟悉在使用它时必须遵守的规则:
- 方法必须是
public
的 - 不能从同一类中的方法中调用它(自调用)
- 如果使用返回类型,必须将其包装在
Future<T>
中,例如CompletableFuture<T>
因此,您会得到:
CompletableFuture<List<String>> categories = this.productServiceImpl.getAllCategories();
进一步处理由Future
的实现驱动,您可以阻塞执行或将更多的Future
连接到单个响应中...
英文:
As long as you use @Async
annotation, you have to get familiar with the rules that have to be respected while using it:
- The method must be
public
only - It cannot be called from a method within the same class (self-invocation)
- If a return type is used, it has to be wrapped in
Future<T>
, ex.CompletableFuture<T>
So you get:
CompletableFuture<List<String>> categories = this.productServiceImpl.getAllCategories();
The further handling is driven by the implementation of Future
where you can either block the execution or join
more of them into a single response...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论