Say I have two Request Scoped classes/components, RedCar and BlueCar, that both implement Car. Say I also have two Request Scoped classes/components RedBoat and BlueBoat, that both implement Boat. The logic that decides between Red and Blue for each class/component is the same. (specifically, this logic would come from information on the HttpServletRequest on the Spring MVC Model). Classes that use Boats and Cars will interact with the interface, aka they do not care about the color. Is there a way to create a single component/piece of logic that would decide which class for Spring to inject (Instead of something that decides which Car to inject, and something separate that decides what Boat to inject). Solutions I have considered (and their problems):
- Use a Spring @Qualifier, aka @IfRed or @IfBlue. The main problem is that I cannot find a way to do this on a request by request basis (I don't believe it is possible)
- Use a custom generic type, aka ColorPicker (for example ColorPicker). However, in this scenario the Car must still have some factory method to choose between implementations.
Aucun commentaire:
Enregistrer un commentaire