春季动态注射 [英] Dynamic injection in Spring
本文介绍了春季动态注射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个税收实施类IndianTaxCalculation
和USTAxCalculation
,它们具有calculateTax()
方法的实施.我需要根据用户登录名(无论是印度还是美国)动态填充类,因此应调用相应的Tax计算方法.我如何在春季实现这一目标?
I have 2 tax implementation classes IndianTaxCalculation
and USTAxCalculation
which have implementaions for calculateTax()
method. I need to populate the class dynamically based on user login whether Indian or US, the respective Tax calculation method should call. How can i achieve this in Spring?
推荐答案
尝试如下操作:
@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);
}
}
这篇关于春季动态注射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文