简体   繁体   中英

Guice dependency injection choosing one of two implementations based on configuration

I have two implementations of my service interface,

public interface Service {
   public ServiceResponse get();
}

One being a Mock implementation which returns a stubbed ServiceResponse and the other doing some real work with some unavailable downstream services.

Although both my RealService and MockService are ready I can only use the mock. And I want that instance to be injected depending on an environment variable. My question is how to achieve this?

Thanks

You can make an implementation of the Guice Provider interface that accesses the environment variable to determine which instance to provide. And then make injection use this provider by using ProviderBindings .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM