在我的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 consumedI 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.
更多推荐
发布评论