quarkus - 从 Quarkus 扩展中公开 CDI bean
问题描述
我正在尝试实现一个基于运行时配置的 Quarkus 扩展,它提供了一个 SecurityIdentityAugmentor。
部署:
我的处理器
@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
private MyMarkerConfiguredBuildItem setupAugmentor(MyRecorder recorder, MyAugmentorConfig config, BeanContainerBuildItem beanContainer) {
recorder.sertConfig(config, beanContainer.getValue();
return new MyMarkerConfiguredBuildItem ();
}
运行:
- 我的录音机:
public void setConfig(MyAugmentorConfig config, BeanContainer beanContainer) {
beanContainer.instance(MyProducer.class).setConfig(config);
}
- 我的制片人:
@ApplicationScoped
public class MyProducer {
public void setConfig(MyConfig config) {
this.config = config;
}
@Produces
@ApplicationScoped
public MyAugmentor createAugmentor() {
return new MyAugmentor(this.config);
}
}
在我的客户端应用程序中生成 MyAugmentor 实例的唯一方法是在扩展的运行时模块中添加 beans.xml。但是我在 github 存储库的其他扩展中看不到 beans.xml。有人能指出我正确的方向吗?
解决方案
您只需要使用构建步骤在扩展的部署模块中注册您的生产者 bean(因为它不会被自动发现,因为扩展没有被索引):
@BuildStep
public AdditionalBeanBuildItem producer() {
return new AdditionalBeanBuildItem(MyProducer.class);
}
生产者必须在您的扩展的运行时模块中,您可以在其中注入您的配置。
@ApplicationScoped
public class MyProducer {
@Inject MyConfig config;
@Produces
@ApplicationScoped
public MyAugmentor createAugmentor() {
return new MyAugmentor(this.config);
}
}
此构造用于许多现有扩展,您可以在此处查看我的 Firestore 扩展示例:https ://github.com/quarkiverse/quarkus-google-cloud-services/tree/master/firestore
推荐阅读
- c++ - 从串口读取的数据长度始终为 1
- stripe-payments - 路由号码的元素
- mariadb - MariaDB 查询双数据类型
- javascript - 如何在 then() 中使用异步函数?
- unity3d - 如何使用 LeanTween 淡化 TextMesh alpha?
- python - gpflow model.elbo 抛出 KeyError 和 NotImplementedError
- ios - 在 SceneKit 中使用 GIFU 库播放 GIF 会导致应用程序 UI 冻结任何解决方案?UIView Animated 从后台线程调用
- r - 用多行绘制行注释
- google-chrome-devtools - 是否有任何选项可以从 node.js 以编程方式启动 Profiler?
- java - 使用 java.time 将“ddMMMyyyy”格式的字符串解析为 java.sql.Date