I have to implement services for different types of contracts. At first, we thought about creating a spring service that would redirect to a type specific service using if else structure.
There must be a better solution with spring.
Is there an easy way to dynamically inject a service per type using spring knowing that there would be an abstract service and one implementation per type.
Example:
@Inject ContractService contractService;
...
Contract iContract = new InsuranceContract(...);
Contract sContract = new SaleContract(...);
...
contractService.save(sContract);
contractService.save(iContract);
The ContractService would be abstract and spring would get the right service depending on the contract type.
Regards
Spring is not suited for your domain logic, even though you can achieve it by overriding certain methods in your class which Spring would automatically call before assigning the appropriate bean, but I wouldn’t recommend it to you, just because plain java code is better suited for this type of tasks.
I would recommend you to go for the solution you originally suggested
If you think about it, in your underlying problem, you have to deal with a if/else if scenario, where you would rather see this, in XML or in Java? I think in general is better to see this in Java and leave Spring for what is best at.