How can i choose dependency without if else condition.
Suppose i have a interface:
public interface A{
String doSomething(String req);
}
there are two service implements A
:
@Component
public class FirstImpl implements A{
@override
String doSomething(String req){
return "something";
}
}
and:
@Component
public class SecondImpl implements A
@override
String doSomething(String req){
return "something";
}
}
Now i create a ServiceAdapter class:
@Component
public class ServiceAdapter{
@Autowired
private FirstImpl first;
@Autowired
private SecondImpl second;
public getService(String name){
if("name".equals("First")){
return first;
}else if("name".equals("Second")){
return second;
}
}
}
Now call Implementation:
@Service
public class BussinessService{
@Autowired
private AdapterService adapterService;
void doSomething(String name,String req){
return adapterService.getService(name).doSomething();
}
}
Now if i need to create another class which implements A then need to add condition in ServiceAdapter class. Like "Third".equals(name)
return another injected service. For every new service there need to add another if else condition and inject corresponding service. How can i avoid this Adapter class. Spring dynamically choose depenedency from name.
Aucun commentaire:
Enregistrer un commentaire