java 是否可以在@RequiredArgsConstructor(onConstructor = @__(@Autowired)) 中添加限定符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38549657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is it possible to add qualifiers in @RequiredArgsConstructor(onConstructor = @__(@Autowired))?
提问by Pau
If I wanted to use the annotation @Qualifier
on a constructor dependency injection, I would have something like the following:
如果我想@Qualifier
在构造函数依赖注入上使用注解,我会有如下内容:
public class Example {
private final ComponentExample component;
@Autowired
public Example(@Qualifier("someComponent") ComponentExample component) {
this.component = component;
}
}
I know lombok annotation to reduce boilerplate code and don't have to include a constructor would be as follows: @RequiredArgsConstructors(onConstructor=@__(@Inject))
but this only works with properties without qualifiers.
我知道 lombok 注释可以减少样板代码并且不必包含构造函数,如下所示:@RequiredArgsConstructors(onConstructor=@__(@Inject))
但这仅适用于没有限定符的属性。
Anyone know if it is possible to add qualifiers in @RequiredArgsConstructor(onConstructor = @__(@Autowired))
?
有谁知道是否可以添加限定符@RequiredArgsConstructor(onConstructor = @__(@Autowired))
?
回答by Nikola Yovchev
EDIT:
编辑:
It is FINALLYPOSSIBLEto do so! You can have a service defined like this:
这是FINALLY可能的这样做!您可以像这样定义一个服务:
@Service
@RequiredArgsConstructor
public class SomeRouterService {
@NonNull private final DispatcherService dispatcherService;
@Qualifier("someDestination1") @NonNull private final SomeDestination someDestination1;
@Qualifier("someDestination2") @NonNull private final SomeDestination someDestination2;
public void onMessage(Message message) {
//..some code to route stuff based on something to either destination1 or destination2
}
}
Provided that you have a lombok.config file like this in the root of the project:
假设您在项目的根目录中有一个像这样的 lombok.config 文件:
# Copy the Qualifier annotation from the instance variables to the constructor
# see https://github.com/rzwitserloot/lombok/issues/745
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier
This was recently introduced in latest lombok 1.18.4, I wrote about it in my blogpost, and I am proud to say I was one of the main driving forces pushing for the implementation of the feature.
这是最近在最新的 lombok 1.18.4 中引入的,我在我的博文中写过它,我很自豪地说我是推动该功能实现的主要驱动力之一。
- The blog post where the issue is discussedin detail
- The original issue on github
- And a small github project to see it in action
- 详细讨论该问题的博客文章
- github上的原始问题
- 和一个小的github 项目来看看它的实际效果
回答by Алексей Виноградов
You may use spring trick to qualify field by naming it with desired qualifier without @Qualifier annotation.
您可以使用 spring 技巧通过使用所需的限定符命名字段而不使用 @Qualifier 注释来限定字段。
@RequiredArgsConstructor
public class ValidationController {
//@Qualifier("xmlFormValidator")
private final Validator xmlFormValidator;
回答by kecso
For me it seems the
对我来说似乎
@RequiredArgsConstructor(onConstructor=@__(@Autowired))
is working too (probably I'm using newer lombok?)
也在工作(可能我正在使用更新的龙目岛?)