として豆の1をマーク考えてみましょう、私はこの私のJava Springアプリケーションで@Primary
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
書かれているが疎結合自分のアプリケーションを作るのインターフェースでありますこのインターフェイスの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注釈が動作しない理由を私は疑問に思って?これはあなたの問題を解決する必要があり
@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;
}
}
と
@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;
}
}
:
あなたの 'BinarySearchImpl'インスタンスはどのように作成されますか?フィールドインジェクションを使用しているので、なぜそれにコンストラクタparamが必要ですか? – Leffchik
あなたが引用した「検討する」の前に、もう少しテキストがあります。質問にもそれを含めてください。 – slim