@Async在Spring Boot应用程序中给我报错。

huangapple go评论127阅读模式
英文:

@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(&quot;threadPoolTaskExecutor&quot;)
    public TaskExecutor getAsyncExecutor() {
        final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);
        executor.setMaxPoolSize(1000);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setThreadNamePrefix(&quot;Async-&quot;);
        return executor;
    }

}

This is service class:

@Component
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductRepository productRepository;

    @Autowired
    private ProductHandler productHandler;

    @Async
    @Override
    public List&lt;String&gt; getAllCategories() {
        final List&lt;String&gt; finalList = new ArrayList&lt;&gt;();
        return finalList;
    }

}

This is the controller class:

@RestController
public class ProductResource {

    @Autowired
    private ProductServiceImpl productServiceImpl;

    @GetMapping(&quot;/categories&quot;)
    public ResponseEntity&lt;List&lt;String&gt;&gt; getAllCategories() {
        return new ResponseEntity&lt;&gt;(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注解,就必须熟悉在使用它时必须遵守的规则:

因此,您会得到:

CompletableFuture&lt;List&lt;String&gt;&gt; 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:

So you get:

CompletableFuture&lt;List&lt;String&gt;&gt; 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...

huangapple
  • 本文由 发表于 2020年4月10日 14:02:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61134832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定