java 考虑将其中一个 bean 标记为 @Primary
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47262363/
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
Consider marking one of the beans as @Primary
提问by Jeff
In my Java spring application, I have
在我的 Java spring 应用程序中,我有
public class BinarySearchImpl {
@Autowired
@Qualifier("Quick")
SortAlgorithem sorter;
Log log=LogFactory.getLog(BinarySearchImpl.class);
public BinarySearchImpl(SortAlgorithem sorter) {
log.info("Binary Search Bean is created");
this.sorter=sorter;
}
SortAlgorithem
is an interface which makes my application loosely coupled:
SortAlgorithem
是一个使我的应用程序松散耦合的接口:
public interface SortAlgorithem {
public int[] sort(int[] arrayNumbers);
}
And then there are 2 implementations for this interface. One is BubbleSort
:
然后这个接口有2个实现。一个是BubbleSort
:
@Component
@Qualifier("Bubble")
public class BubbleSort implements SortAlgorithem {
Log log=LogFactory.getLog(BubbleSort.class);
public int[] sort(int[] numbers) {
log.info("Bubble sort is called");
return numbers;
}
}
and the other is QuickSort
:
另一个是QuickSort
:
@Component
@Qualifier("Quick")
//@Primary
public class QuickSort implements SortAlgorithem{
Log log= LogFactory.getLog(QuickSort.class);
public int[] sort(int[] numbers) {
log.info("Quick Sort is called");
return numbers;
}
}
At the end, when I call my app it complains with this message:
最后,当我调用我的应用程序时,它会抱怨以下消息:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
I am wondering... Why @Qualifier
annotation does not work?
我想知道......为什么@Qualifier
注释不起作用?
采纳答案by slim
You have annotated a field with @Autowired
and @Qualifier
, but you have also created a constructor which sets the field.
您已使用@Autowired
and注释了一个字段@Qualifier
,但您还创建了一个用于设置该字段的构造函数。
I think that Spring is using the constructor, but doesn't automatically know that the constructor parameter corresponds to the annotated field.
我认为 Spring 正在使用构造函数,但不会自动知道构造函数参数对应于带注释的字段。
So move the annotations into the constructor declaration:
因此,将注释移至构造函数声明中:
private SortAlgorithm sorter;
@Autowired
public BinarySearchImpl(@Qualifier("quick") SortAlgorithm sorter) {
this.sorter = sorter;
}
Alternatively, you could use a zero-arg constructor, keep your field annotation and let Spring inject using reflection. However in my opinion constructor-injection is better -- it allows you to unit test cleanly, without involving Spring or reflection.
或者,您可以使用零参数构造函数,保留字段注释并让 Spring 使用反射注入。但是在我看来构造函数注入更好——它允许您干净地进行单元测试,而无需涉及 Spring 或反射。
As other answers point out, there are other ways to disambiguate autowired beans -- and the Spring docs explain them all -- but using qualifiers like this does work.
正如其他答案所指出的那样,还有其他方法可以消除自动装配 bean 的歧义——Spring 文档对它们进行了全部解释——但是使用像这样的限定符确实有效。
回答by Sujal Mandal
try
尝试
@Component("qualifier_name")
回答by Chaitanya
You have to declare your components like this:
你必须像这样声明你的组件:
@Component("Bubble")
public class BubbleSort implements SortAlgorithem {
Log log=LogFactory.getLog(BubbleSort.class);
public int[] sort(int[] numbers) {
log.info("Bubble sort is called");
return numbers;
}
}
and
和
@Component("Quick")
public class QuickSort implements SortAlgorithem{
Log log= LogFactory.getLog(QuickSort.class);
public int[] sort(int[] numbers) {
log.info("Quick Sort is called");
return numbers;
}
}
This should solve your issue.
这应该可以解决您的问题。
回答by ThinkTank
Hi Sorry for coming late to the party.
I had 2 service class which were implementing a single interface like this,So
I have done this with @Component("name")
and it worked.
though it is still a mystery why @Qualifier("name")
did not worked?
嗨,很抱歉来晚了。我有 2 个服务类,它们正在实现这样的单个接口,所以我已经完成了@Component("name")
它并且它起作用了。虽然它仍然是一个谜为什么@Qualifier("name")
没有奏效?
Code:
代码:
@Service("signup")
//@Qualifier("signup")
public class SignUpGenerator implements ITestCaseGenerator {
}
@Component
public class SelUtil {
@Autowired
@Qualifier("login")
ITestCaseGenerator login;
@Autowired
@Qualifier("signup")
ITestCaseGenerator signUp;
}
回答by Shrikant Wandhare
The correct approach:
正确的做法:
public interface SortAlgorithem {
public int[] sort(int[] arrayNumbers);
}
@Component("Bubble")
public class BubbleSort implements SortAlgorithem {
Log log = LogFactory.getLog(BubbleSort.class);
public int[] sort(int[] numbers) {
log.info("Bubble sort is called");
return numbers;
}
}
@Primary
@Component("Quick")
public class QuickSort implements SortAlgorithem {
Log log = LogFactory.getLog(QuickSort.class);
public int[] sort(int[] numbers) {
log.info("Quick Sort is called");
return numbers;
}
}
and then you need to use your implementations like this:
然后你需要像这样使用你的实现:
@Autowired
@Qualifier(value = "Bubble")
private SortAlgorithem bubbleSort;
@Autowired
@Qualifier(value = "Quick")
private SortAlgorithem quickSort;