考虑将其中一个bean标记为@Primary(Consider marking one of the beans as @Primary)

编程入门 行业动态 更新时间:2024-10-24 02:38:47
考虑将其中一个bean标记为@Primary(Consider marking one of the beans as @Primary)

在我的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是一个让我的应用程序SortAlgorithem的接口:

public interface SortAlgorithem { public int[] sort(int[] arrayNumbers); }

然后这个接口有2个实现

@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; } }

然后快速排序:

@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; } }

最后,当我打电话给我的应用程序时,我会打电话:

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

我想知道为什么@Qualifier anotation不起作用?

In my Java spring application, I have

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:

public interface SortAlgorithem { public int[] sort(int[] arrayNumbers); }

And then there are 2 implementations for this interface. One is 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:

@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?

最满意答案

您已使用@Autowired和@Qualifier注释了一个字段,但您还创建了一个用于设置字段的构造函数。

我认为Spring正在使用构造函数,但不会自动知道构造函数参数对应于带注释的字段。

因此,将注释移动到构造函数声明中:

private SortAlgorithm sorter; @Autowired public BinarySearchImpl(@Qualifier("quick") SortAlgorithm sorter) { this.sorter = sorter; }

或者,您可以使用零参数构造函数,保留您的字段注释并让Spring使用反射注入。 但是在我看来构造函数注入更好 - 它允许你干净地进行单元测试,而不涉及Spring或反射。

正如其他答案所指出的,还有其他方法可以消除自动装配的bean的歧义 - 而Spring文档解释了所有这些 - 但是使用这样的限定符确实有效。

You have annotated a field with @Autowired and @Qualifier, but you have also created a constructor which sets the field.

I think that Spring is using the constructor, but doesn't automatically know that the constructor parameter corresponds to the annotated field.

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.

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.

更多推荐

本文发布于:2023-07-16 13:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1129015.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:其中一个   标记   bean   beans   Primary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!