Quarkus可配置的bean带有自动注射
是否可以与具有不同配置的多个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在Quarkus中是不可能的。如果您在生产者方法中手动创建一个实例,则无需对其执行依赖项注入,并且必须自己完全构建实例。
您可能希望将一些依赖项注入生产者方法,这是可能的,因为每个生产者方法参数实际上是一个注入点:(
完整的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:
(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.)