Quarkus可配置的bean带有自动注射

发布于 2025-01-26 12:18:27 字数 664 浏览 2 评论 0原文

是否可以与具有不同配置的多个Bean拥有相同的类。那仍然可以自动注射吗?我尝试关注:

class Config1 {
  @Scope1
  @Produces
  @ApplicationScoped
  public Parser parser1() {
    return new MySpecialParser("key1");
  }
}

class Config2 {
  @Scope2
  @Produces
  @ApplicationScoped
  public Parser parser2() {
    return new MySpecialParser("key2");
  }
}

class MySpecialParser extends Parser {
  // ...
}

abstract class Parser {
  @Inject
  @Localized("en")
  Messages messages;

  Parser(String config) {
    // ...
  }
}

类似的事情。但是,当我注入解析器 消息始终为空。

在春季存在的是类似用例的AutoWireCapableBeanFactory。但是,也许我最初的想法是错误的,在夸克斯,我应该以不同的方式做。

有人一个线索吗?

is it possible to have the same class as multiple beans with different configurations. That still get auto injection? I tried following:

class Config1 {
  @Scope1
  @Produces
  @ApplicationScoped
  public Parser parser1() {
    return new MySpecialParser("key1");
  }
}

class Config2 {
  @Scope2
  @Produces
  @ApplicationScoped
  public Parser parser2() {
    return new MySpecialParser("key2");
  }
}

class MySpecialParser extends Parser {
  // ...
}

abstract class Parser {
  @Inject
  @Localized("en")
  Messages messages;

  Parser(String config) {
    // ...
  }
}

Something like this. But when I inject the parser the messages are always null.

In Spring exists AutowireCapableBeanFactory for use case like that. But maybe my initial thought is just wrong and in Quarkus I should do it differently.

Has someone a clue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光瘦了 2025-02-02 12:18:27

这在Quarkus中是不可能的。如果您在生产者方法中手动创建一个实例,则无需对其执行依赖项注入,并且必须自己完全构建实例。

您可能希望将一些依赖项注入生产者方法,这是可能的,因为每个生产者方法参数实际上是一个注入点:(

class Config1 {
  @Scope1
  @Produces
  @ApplicationScoped
  public Parser parser1(@Localized("en") Messages messages) {
    return new MySpecialParser("key1", messages);
  }
}

完整的CDI中有设施可以让您做类似的事情,但它们与quarkus build不兼容 - 以时为导向的体系结构和Quarkus无法实现它们。)

This is not possible in Quarkus. If you create an instance manually, which you do in the producer methods, no dependency injection is performed on it and you have to fully construct the instance yourself.

You may want to inject some dependencies into the producer methods, which is possible, as each producer method parameter is actually an injection point:

class Config1 {
  @Scope1
  @Produces
  @ApplicationScoped
  public Parser parser1(@Localized("en") Messages messages) {
    return new MySpecialParser("key1", messages);
  }
}

(There are facilities in full CDI that let you do something similar, but they are not compatible with the Quarkus build-time oriented architecture and Quarkus doesn't implement them.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文