【发布时间】:2017-06-06 18:56:04
【问题描述】:
我必须对制片人:
@Produces
public IPaymentGateway getStripePaymentGateway(@StripeApiKey final String apiKey) {
return new StripeFluentAPI(apiKey);
}
@Produces
public IPaymentGateway getStripePaymentGatewayProxy() {
IPaymentGateway gateway = mock(IPaymentGateway.class);
ICustomer customer = mock(ICustomer.class);
when(gateway.customer()).thenReturn(customer);
return gateway;
}
第一个返回我的IPaymentGateway 的真实实现。另一方面,第二个返回一个代理对象。
我正在使用@ApplicationScoped 对象来了解是否必须启用或禁用网关:
@ApplicationScoped
public class ConfigurationResources {
public boolean isPaymentGatewayEnabled() {
return paymentGatewayEnabled;
}
}
所以,我想知道如何根据isPaymentGatewayEnabled的值选择on或其他生产者。
【问题讨论】:
-
您可以使用一个方法生成
IPaymentGateway,并将ConfigurationResources添加为该方法的参数。然后,您可以根据isPaymentGatewayEnabled值生成“真实”bean 或模拟。
标签: jakarta-ee cdi