Skip to content

Exceptions

Yash edited this page Aug 10, 2018 · 1 revision
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
public interface SortAlgorithem {
    public int[] sort(int[] arrayNumbers);
}
@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;
    }
}

Solution:

public class BinarySearchImpl {

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

Clone this wiki locally