原创转载请注明出处:http://agilestyle.iteye.com/blog/2330133
考虑一个如下的场景:如图所示,两个Invoice的Service实现同一个接口,在Resource下调用两个服务的话,需要使用@Qualifier注解来确保注入的Bean是唯一的。
IBatchApprovalService.java
public interface IBatchApprovalService {
...
}
IInvoiceApprovalService.java
public interface IInvoiceApprovalService {
...
}
PaymentBatchApprovalService.java
@Service
public class PaymentBatchApprovalService implements IBatchApprovalService {
...
}
InvoiceDetailApprovalService.java
@Service("invoiceDetailApprovalService")
public class InvoiceDetailApprovalService implements IInvoiceApprovalService {
...
}
InvoiceHeaderApprovalService.java
@Service("invoiceHeaderApprovalService")
public class InvoiceHeaderApprovalService implements IInvoiceApprovalService {
...
}
PaymentApprovalResource.java
@Component
@Scope("request")
@Path("/payment_approval")
public class PaymentApprovalResource {
...
@Inject
@Qualifier("invoiceDetailApprovalService")
private IInvoiceApprovalService invoiceDetailApprovalService;
@Inject
@Qualifier("invoiceHeaderApprovalService")
private IInvoiceApprovalService invoiceHeaderApprovalService;
@Inject
private IBatchApprovalService paymentBatchApprovalService;
}
本文介绍了一个具体的Spring框架中依赖注入的示例。通过两个不同的Invoice Service实现展示如何使用@Qualifier注解确保注入正确的Bean。这有助于理解在多个实现存在的情况下如何精确指定所需的组件。
5990

被折叠的 条评论
为什么被折叠?



