Quarkus为什么不扫描Jakarta EE的Bean?

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

Why Quarkus doesn't scan JakartaEE beans?

问题

我正在进行从部署在 JBoss 上的 JavaEE/JakartaEE(版本 7.0)应用迁移至 Quarkus 的工作。

我已删除了所有的 JEE 和 JBoss 依赖,并用 Quarkus 依赖进行了替换。现在,当我按照 Quarkus 文档中指定的命令 mvn compile quarkus:dev 启动应用程序时,我收到了许多类似以下错误的错误消息:

[1] 无法满足类型为 com.freesoft.diba.jeeop.cert_proxy.acme.database.NonceRepository 且限定符为 [@Default] 的未满足依赖项
[ERROR]         - Java 成员:com.freesoft.diba.jeeop.cert_proxy.acme.AcmeProtocolFeature#nonceRepository
[ERROR]         - 声明于 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 标准?

英文:

I am working on a migration from a JavaEE/JakartaEE (v. 7.0) application deployed on JBoss to Quarkus.

I've removed all the JEE and JBoss dependencies and replaced them with Quarkus dependencies. Now, when I'm starting the application by using the command: mvn compile quarkus:dev specified in Quarkus documentation, I'm receiving a lot of errors like the following one:

[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] 

The class AcmeProtocolFeature is the following one:

@Provider
public class AcmeProtocolFeature implements DynamicFeature {

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

The class NonceRepository is the following one:

public class NonceRepository {

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

In the previous version (the JEE one) of the application everything worked just well. I'm wondering why this is not working anymore as expected, because as long as I know, Quarkus implements all JavaEE/JakartaEE standards?!

答案1

得分: 4

Quarkus文档中所述,没有标有bean定义注解的类将不会被发现。

> 没有标有bean定义注解的bean类将不会被发现。这个行为由CDI定义。但是,即使声明类没有用bean定义注解进行标注,生产者方法、字段和观察者方法仍然会被发现(这个行为与CDI中的定义不同)。

在JavaEE/JakartaEE中,如果一个类没有指定任何bean定义注解,那么它默认会被认为是用@Dependent进行标注的。基本上,这就是为什么JavaEE/JakartaEE版本的应用程序能够正常工作,而Quarkus版本的应用程序则完全无法工作的原因。

解决方案将是在您希望进一步注入的每个类的顶部明确指定一个bean定义注解,特别是在这种情况下,类NonceRepository至少应该用@Dependent注解进行标注。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月17日 15:37:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63933338.html
匿名

发表评论

匿名网友

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

确定