简体   繁体   English

应用程序无法使用 @Async 注释启动

[英]Application failed to start with @Async annotation

I am trying to run a function in background asynchronously.我正在尝试在后台异步运行一个函数。 For this I am trying Spring's @Async annotation but my application is unable to start after putting this annotation on the function.为此,我正在尝试 Spring 的 @Async 注释,但是在将这个注释放在函数上后,我的应用程序无法启动。

I tried我试过

@EnableAsync(proxyTargetClass = true) @EnableAsync(proxyTargetClass = true)

but still no luck.但仍然没有运气。

Below is the message I am getting on application start.以下是我在应用程序启动时收到的消息。


APPLICATION FAILED TO START应用程序无法启动


Description:描述:

The bean 'MyBatchSyncProcessor' could not be injected as a 'com.abcservice.MyBatchSyncProcessor' because it is a JDK dynamic proxy that implements: com.abcservice.BaseSyncProcessor bean 'MyBatchSyncProcessor' 无法作为 'com.abcservice.MyBatchSyncProcessor' 注入,因为它是一个 JDK 动态代理,实现了:com.abcservice.BaseSyncProcessor

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.考虑将 bean 作为其接口之一注入,或者通过在 @EnableAsync 和/或 @EnableCaching 上设置 proxyTargetClass=true 来强制使用基于 CGLib 的代理。

Please help me out in getting this application up and running.请帮助我启动并运行此应用程序。

不要注入您的服务实现 ( MyBatchSyncProcessor ) - 注入其接口BaseSyncProcessor

Although the first answer is correct, it doesn't really touches on why :虽然第一个答案是正确的,但它并没有真正涉及为什么

From https://www.fatalerrors.org/a/0dx20j8.html :来自https://www.fatalerrors.org/a/0dx20j8.html

Async registers a Configuration configuration class with Spring using Spring's ImportSelector method, and injects a BeanPostProcessor, which calls the postProcessAfterInitialization method after the bean is instantiated to generate dynamic proxies for the object ( if the target class implements an interface, the JDK proxy is used by default, otherwise CGLib is used ) Async使用Spring的ImportSelector方法向Spring注册一个Configuration配置类,并注入一个BeanPostProcessor,在bean实例化后调用postProcessAfterInitialization方法为对象生成动态代理(如果目标类实现了接口,则JDK代理由默认,否则使用 CGLib )

When you combine this with :当您将其与:

( https://www.tutorialspoint.com/difference-between-jdk-dynamic-proxy-and-cglib-proxy-in-spring ) ( https://www.tutorialspoint.com/difference-between-jdk-dynamic-proxy-and-cglib-proxy-in-spring )

JDK dynamic proxy is available with the JDK. JDK 动态代理随 JDK 一起提供。 It can be only proxy by interface so target class needs to implement interface.它只能通过接口代理,因此目标类需要实现接口。 In your is implementing one or more interface then spring will automatically use JDK dynamic proxies.在您实现一个或多个接口时,spring 将自动使用 JDK 动态代理。

Now it becomes apparent that once you add the @EnableAsync aspect, you need to inject the interface BaseSyncProcessor rather than its implementation MyBatchSyncProcessor , in beans that use the @Async annotation.现在很明显,一旦添加了@EnableAsync方面,就需要在使用@Async注释的 bean 中注入接口BaseSyncProcessor而不是其实现MyBatchSyncProcessor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM