How to inject an interface implementation based on annotations at runtime using Google Guice

34 views
Skip to first unread message

Rudolf Visagie

unread,
Aug 23, 2014, 5:52:53 AM8/23/14
to google...@googlegroups.com
I have the following scenario:

    public interface ServiceClientAdapter {
       
SomeData getSomeData()
   
}


   
@LegacyServiceClientAdapter
   
public class MyLegacyServiceClientAdapterImpl implements ServiceClientAdapter {
       
public SomeData getSomeData() {
           
// implementation          
       
}
   
}
   
   
@NewServiceClientAdapter
   
public class MyNewServiceClientAdapterImpl implements ServiceClientAdapter  {
       
public SomeData getSomeData() {
           
// implementation          
       
}    
   
}


   
public class BusinessLogic {
       
@Inject
       
private ServiceClientAdapter serviceClientAdapter;
   
}



LegacyServiceClientAdapter and NewServiceClientAdapter are custom annotations. 

The implementation for the serviceClientAdapter field will be determined at runtime by whether the user has been migrated from the legacy to the new service or not. 

What is the best way to accomplish this dependency injection using Google Guice?

Take into account that different BusinessLogic classes will exist, each with their own (different) ServiceClientAdapter-like interface and corresponding legacy and new implementation classes.

Ideally this should be done with a piece of framework code that can be used across all use cases.

Chad Urso McDaniel

unread,
Aug 26, 2014, 4:52:26 PM8/26/14
to google...@googlegroups.com

On Sat, Aug 23, 2014 at 2:52 AM, Rudolf Visagie <rudolf....@gmail.com> wrote:
What is the best way to accomplish this dependency injection using Google Guice?

Take into account that different BusinessLogic classes will exist, each with their own (different) ServiceClientAdapter-like interface and corresponding legacy and new implementation classes.

Ideally this should be done with a piece of framework code that can be used across all use cases.

Does injecting a Provider<ServiceClientAdapter> give you enough indirection to code the provider behavior yourself?
Reply all
Reply to author
Forward
0 new messages