Dynamic injection in Spring

后端 未结 3 786
予麋鹿
予麋鹿 2021-01-29 05:00

I have 2 tax implementation classes IndianTaxCalculation and USTAxCalculation which have implementaions for calculateTax() method. I need

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-29 05:29

    Try something like this:

    @Autowired
    @Qualifier("indian")
    private TaxCalculation indianTax;
    @Autowired
    @Qualifier("us")
    private TaxCalculation usTax;
    
    
    public void calculateTaxes(Client client) {
       if (client.isFromIndia()) {
          indiaTax.calculate(client);
       } else if (client.isFromUS()) {
          usTax.calculate(client);
       }
    }
    

提交回复
热议问题