首页 > 解决方案 > 为什么 Quarkus 不扫描 JakartaEE bean?

问题描述

我正在从部署在 JBoss 上的 JavaEE/JakartaEE (v. 7.0) 应用程序迁移到 Quarkus。

我已经删除了所有 JEE 和 JBoss 依赖项,并将它们替换为 Quarkus 依赖项。现在,当我使用mvn compile quarkus:devQuarkus 文档中指定的 command: 启动应用程序时,我收到很多错误,如下所示:

[1] Unsatisfied dependency for type com.freesoft.diba.jeeop.cert_proxy.acme.database.NonceRepository and qualifiers [@Default]
[ERROR]         - java member: com.freesoft.diba.jeeop.cert_proxy.acme.AcmeProtocolFeature#nonceRepository
[ERROR]         - declared on CLASS bean [types=[java.lang.Object, com.freesoft.diba.jeeop.cert_proxy.acme.AcmeProtocolFeature], qualifiers=[@Default, @Any], target=com.freesoft.diba.jeeop.cert_proxy.acme.AcmeProtocolFeature] 

该课程AcmeProtocolFeature如下:

@Provider
public class AcmeProtocolFeature implements DynamicFeature {

    @Inject
    Logger logger;
    @Inject
    PolicyHandler policyHandler;
    @Inject
    NonceRepository nonceRepository
    [...]

该课程NonceRepository如下:

public class NonceRepository {

    @Inject
    @PersistenceContext(unitName = "acme")
    EntityManager em;

在应用程序的先前版本(JEE 版本)中,一切都运行良好。我想知道为什么这不再像预期的那样工作了,因为据我所知,Quarkus 实现了所有 JavaEE/JakartaEE 标准?!

标签: javajakarta-eejava-ee-7quarkus

解决方案


As it is stated in Quarkus documentation, the classes that are not having a bean defining annotation are not discovered.

Bean classes that don’t have a bean defining annotation are not discovered. This behavior is defined by CDI. But producer methods and fields and observer methods are discovered even if the declaring class is not annotated with a bean defining annotation (this behavior is different to what is defined in CDI)

In JavaEE/JakartaEE, if a class did not have specified any bean defining annotation, then the it will be considered by default annotated with @Dependent, so basically, this is why the JavaEE/JakartaEE version of the application worked well, and the Quarkus one did not work at all.

The solution would be to explicitly specify a bean defining annotation on top of each class that you want to inject further, specifically in this scenario, the class NonceRepository should be annotated at least with the @Dependent annotation.


推荐阅读