3 回答

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊
我可以想到兩種方法。第一種方法是命名類(在 @Component 之后)并使用上下文,嘗試獲取 bean。您還可以使用 @Qualifier 命名 bean
@Component("A")
public class A{
public String wish(int timeHr){
//some logic of time based wish
return "Hello A"+" good morning";
}
@Component("B")
public class B{
public String wish(int timeHr){
//some logic of time based wish
return "Hello B"+" good morning";
}
}
@Component("C")
public class C{
public String wish(int timeHr){
//some logic of time based wish
return "Hello C"+" good morning";
}
}
@Service
public class MyService{
@Autowired
private ApplicationContext context;
void myMethod() {
A a = (A) context.getBean("A");
System.out.println(a.wish(123));
}
}
第二種方法是讓你所有的愿望類實(shí)現(xiàn)一個(gè)接口并遍歷這個(gè)接口的所有實(shí)現(xiàn) bean 并找到正確的 bean
@Component
public class A implements Wisher{
public String wish(int timeHr){
//some logic of time based wish
return "Hello A"+" good morning";
}
@Component
public class B implements Wisher{
public String wish(int timeHr){
//some logic of time based wish
return "Hello B"+" good morning";
}
}
@Component
public class C implements Wisher{
public String wish(int timeHr){
//some logic of time based wish
return "Hello C"+" good morning";
}
}
public interface Wisher{ public String wish(int time); }
@Service
public class MyService{
@Autowired
private List<Wisher> wishers;
void myMethod() {
String requiredClassName = "A";
Wisher requiredWisher = null;
for (int i = 0; i < wishers.length(); i++) {
Wisher w = wishers.get(i);
if (w.getClass().getName().equals(requiredClassName)) {
requiredWisher = w;
break;
}
System.out.println(w.wish(123));
}
}
}

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
考慮到您正在使用 Spring 實(shí)現(xiàn),所有 bean 都將是 Singleton,并且這些 bean 將在 App 啟動(dòng)時(shí)(加載 ApplicationContext 時(shí))進(jìn)行初始化,然后應(yīng)用程序無法檢查要注入哪個(gè)實(shí)現(xiàn)。
因此,您無法在運(yùn)行時(shí)使用依賴注入有條件地注入 bean
相反,您可以使用如下所示的其他設(shè)計(jì) -
@Service
public class MyService{
private Wisher wisher;
public Wisher setupWisher(String class){
if (class.equals("A")) {
wisher = new A();
}else if(class.equals("B")){
wisher = new B();
}else if(class.equals("C")){
wisher = new C();
}
}
void myMethod(String requestString) {
int timrHr = new Date().getHours();
setupWisher(requestString).wish(timrHr);
}
}

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
String 不是 Spring IoC 框架可管理的 bean,而是類的實(shí)例A,B并且C是您的 beans。
你應(yīng)該做的是聲明類A,B并C作為公共接口的實(shí)現(xiàn),并通過這種類型注入相應(yīng)的 bean:
interface Wisher {
String wish(int timeHr);
}
@Component
public class A implements Wisher {
public String wish(int timeHr){
//some logic of time based wish
return "Hello A"+" good morning";
}
}
@Component
public class B implements Wisher {
public String wish(int timeHr){
//some logic of time based wish
return "Hello B"+" good morning";
}
}
@Component
public class C implements Wisher {
public String wish(int timeHr){
//some logic of time based wish
return "Hello C"+" good morning";
}
}
@Service
public class MyService{
@Autowired
private Wisher a; // instance of "A" or "B", or "C"
void myMethod() {
int timrHr = new Date().getHours();
wisher.wish(timrHr);
}
}
添加回答
舉報(bào)