Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @spring引导应用程序中的Async给我错误_Java_Spring_Multithreading_Spring Boot_Asynchronous - Fatal编程技术网

Java @spring引导应用程序中的Async给我错误

Java @spring引导应用程序中的Async给我错误,java,spring,multithreading,spring-boot,asynchronous,Java,Spring,Multithreading,Spring Boot,Asynchronous,这是主要的应用程序类: @EnableAsync(proxyTargetClass = true) @EnableDiscoveryClient @SpringBootApplication public class ProductApplication { public static void main(final String[] args) { SpringApplication.run(ProductApplication.class, args); }

这是主要的应用程序类:

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

}
@组件
公共类ProductServiceImpl实现ProductService{
@自动连线
产品库产品库;
@自动连线
私人ProductHandler ProductHandler;
@异步的
@凌驾
公共列表getAllCategories(){
最终列表finalList=new ArrayList();
回归终结者;
}
}
这是控制器类:

@RestController
public class ProductResource {

    @Autowired
    private ProductServiceImpl productServiceImpl;

    @GetMapping("/categories")
    public ResponseEntity<List<String>> getAllCategories() {
        return new ResponseEntity<>(this.productServiceImpl.getAllCategories(), HttpStatus.OK);
    }
}
@RestController
公共类产品资源{
@自动连线
私有ProductServiceImpl ProductServiceImpl;
@GetMapping(“/categories”)
公共响应getAllCategories(){
返回新的ResponseEntity(this.productServiceImpl.getAllCategories(),HttpStatus.OK);
}
}
我已经用
@Async
注释了服务实现方法,但我得到了以下错误:

行动:

考虑将bean作为其接口之一注入,或者通过在@enablesync和/或@EnableCaching上设置proxyTargetClass=true来强制使用基于CGLib的代理

如果我尝试注释控制器,我会得到对get请求的空响应。我已经尝试了所有方法,包括将
proxyTargetClass
设置为true。

只要您使用annotation,您就必须熟悉在使用它时必须遵守的规则:

  • 该方法只能是
    public
  • 不能从同一类中的方法调用它(自调用)
  • 如果使用返回类型,则必须将其包装为,例如
所以你得到:

CompletableFuture<List<String>> categories = this.productServiceImpl.getAllCategories();
CompletableFuture categories=this.productServiceImpl.getAllCategories();

进一步的处理是由
Future
的实现驱动的,您可以阻止执行,或者
将更多的
连接到一个响应中…

要么放弃
ProductService
接口,要么开始使用它,使用
ProductService
而不是
productserviceinpl
controller.@M.Deinum您能告诉我为什么不能将异步应用于ProductserviceImpl吗?我没说过。我说你应该要么编程接口(使用接口而不是控制器中的实现),要么放弃接口。重建项目。我自动连接ProductService。我得到了空响应,但当我删除@Async注释时,我得到了响应。你能告诉我怎么修吗?