@Component
@Qualifier("SUCCESS")
public class RandomServiceSuccess implements RandomService{
public String doStuff(){
return "success";
}
}
@Component
@Qualifier("ERROR")
public class RandomServiceError implements RandomService{
public String doStuff(){
throw new Exception();
}
}
@Controller
public class RandomConroller {
@Autowired
private RandomService service;
public String do(){
service.doStuff();
}
}
我需要做的,是基於可從http請求一些自定義的HTTP頭中檢索值,以讓他們交換。謝謝!
爲什麼你不注入兩個,只使用你需要的那個? –
@SotiriosDelimanolis如何在運行時交換實現?注入它們會導致Spring自動掃描並發現它們。 – Bobo