1. Overview
1.概述
In this article, we’ll learn the difference between bean discovery in Quarkus and classic Jakarta EE environments. We’ll focus on how to ensure that Quarkus can discover annotated classes in external modules.
在这篇文章中,我们将学习Quarkus中的bean发现和经典的Jakarta EE环境的区别。我们将重点讨论如何确保Quarkus能够发现外部模块中的注解类。
2. Why Quarkus Needs Indexing
2.为什么Quarkus需要编制索引
One of the main advantages of Quarkus is its extremely fast boot time. To achieve this, Quarkus moves steps like classpath annotation scanning forward from runtime to build-time. For this, we need to announce all dependencies at build-time.
Quarkus的主要优势之一是其启动时间极快。为了实现这一目标,Quarkus将诸如classpath注释扫描等步骤从运行时提前到构建时。为此,我们需要在构建时公布所有的依赖关系。
So, enhancing an application by classpath extension at the runtime environment is not possible anymore. Indexing joins the game when metadata is collected during the build. Indexing means storing metadata within an index file. This allows the application to read it out quickly at startup or whenever it’s needed.
因此,在运行时环境中通过classpath扩展来增强一个应用程序是不可能的。当元数据在构建过程中被收集时,索引也加入了游戏。索引意味着将元数据存储在一个索引文件中。这允许应用程序在启动时或需要时快速读出它。
Let’s examine the difference using a simple sketch:
让我们用一个简单的草图来研究一下其中的差别。
Quarkus uses Jandex to create and read the index.
Quarkus使用Jandex来创建和读取索引。
3. Create an Index
3.创建一个索引
For the classes in our Quarkus project, we don’t have to do anything special – the Quarkus Maven Plugin will generate the index automatically. But, we need to pay attention to the dependencies – the project-internal modules as well as the external libraries.
对于Quarkus项目中的类,我们不需要做任何特别的事情–Quarkus Maven插件会自动生成索引。但是,我们需要注意依赖关系–项目内部的模块和外部的库。
3.1. Jandex Maven Plugin
3.1 Jandex Maven Plugin
The most obvious method to achieve this for our own modules is to use the Jandex Maven Plugin:
对于我们自己的模块,最明显的方法是使用Jandex Maven Plugin。
<build>
<plugins>
<plugin>
<!-- https://github.com/wildfly/jandex-maven-plugin -->
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>make-index</id>
<goals>
<!-- phase is 'process-classes by default' -->
<goal>jandex</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
This plugin creates a “META-INF/jandex.idx” file packaged into the JAR. Quarkus will read out this file when provided by the library at runtime. So, each library that contains such a file implicitly enhances the index.
这个插件创建了一个“META-INF/jandex.idx”文件,被打包到JAR中。当库在运行时提供的时候,Quarkus将读出这个文件。所以,每个包含这样一个文件的库都隐含地增强了索引。
For Gradle builds, we can use the org.kordamp.gradle.jandex plugin:
对于Gradle构建,我们可以使用org.kordamp.gradle.jandex插件。
plugins {
id 'org.kordamp.gradle.jandex' version '0.11.0'
}
3.2. Application Properties
3.2.应用属性
If we cannot modify the dependencies (for example, in the case of external libraries), we need to specify them explicitly in the Quarkus project’s application.properties file:
如果我们不能修改依赖关系(例如,在外部库的情况下),我们需要在Quarkus项目的application.properties文件中明确指定它们。
quarkus.index-dependency.<name>.group-id=<groupId><br/>
quarkus.index-dependency.<name>.artifact-id=<artifactId><br/>
quarkus.index-dependency.<name>.classifier=(optional)
quarkus.index-dependency.<name>.group-id=<groupId><br/>
quarkus.index-dependency.<name>.artifact-id=<artifactId><br/>
quarkus.index-dependency.<name>.classifier=(可选)
3.3. Framework-Specified Implications
3.3.框架的具体影响
Instead of using the Jandex Maven Plugin, a module could also contain a META-INF/beans.xml file. This is actually part of the CDI technology that is adopted into Quarkus with some adjustments, but we are not restricted to using CDI-managed beans only. We could also declare, for example, JAX-RS resources because the scope of the index is the whole module.
不使用Jandex Maven插件,一个模块也可以包含一个META-INF/beans.xml文件。这实际上是CDI技术的一部分,经过一些调整后被采纳到Quarkus中,但我们并不局限于只使用CDI管理的bean。我们也可以声明,例如,JAX-RS资源,因为索引的范围是整个模块。
4. Conclusion
4.总结
This article has determined that Quarkus needs a Jandex index to detect annotated classes at runtime. The index is generated during build time, so the standard technologies do not detect annotated classes added to the classpath after the build.
这篇文章确定Quarkus需要一个Jandex索引来在运行时检测注释类。索引是在构建时生成的,所以标准技术无法检测到在构建后添加到classpath的注释类。
As always, all the code is available over on GitHub. There’s a multi-module project containing a Quarkus application and some dependencies that provide CDI-managed beans.
像往常一样,所有的代码都可以在GitHub上找到。这里有一个多模块项目,包含一个Quarkus应用程序和一些提供CDI管理的bean的依赖项。