I have Module with @Provides method:
class EnvironmentModule extends AbstractModule {
static String CURRENT_PRODUCT
@Override
protected final void configure() {
}
@Provides
static ProcessBuilder get(){
return CURRENT_PRODUCT == 'Product1' ? new Product1ProcessBuilder() : new Product2ProcessBuilder()
}
Class where I Inject ProcessBuilder:
class Operation {
@Inject
Provider<ProcessBuilder> processBuilder
void operate() {
def process = processBuilder.get().build()
}
}
Step how I execute my test:
CURRENT_PRODUCT = 'Product1'operate()give us instance of prodcut1, e.gProduct1ProcessBuilder@26aecf31CURRENT_PRODUCT = 'Product2'operate()give us instance of prodcut2, e.gProduct2ProcessBuilder@4bb4adf7CURRENT_PRODUCT = 'Product1'operate()give us instance of prodcut1, e.gProduct1ProcessBuilder@11544dddCURRENT_PRODUCT = 'Product2'operate()give us instance of prodcut2, e.gProduct2ProcessBuilder@38e46ea
How can I get the same instance in steps 2), 6) and 4), 8) ?
When I annotate @Provides method with @Singleton it always returns the same instance of the first returned product.
When I annotate each class products class with @Singleton then I have each time another product.