Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an instance within a Guice Module

I have this class:

public class CompositeSecurityAuthorizer implements SecurityAuthorizer {
    @inject @CompositeSecurityAuthorizerAnnot
    List<SecurityAuthorizer> authorizers; //Field Injection
}

I want to inject the authorizers field a List<SecurityAuthorizer> value.

In my module , I have the following:

@Override
protected void configure() {
  bind(CompositeSecurityAuthorizer.class).in(Singleton.class);
  bind(StoreAuthorizer.class).in(Singleton.class);
  bind(SecurityAuthorizer.class)
      .annotatedWith(CompositeSecurityAuthorizerAnnot.class)
      .to(CompositeSecurityAuthorizer.class);
}

@Provides @CompositeSecurityAuthorizerAnnot
List<SecurityAuthorizer> provideAuthorizersList()
{
    List<SecurityAuthorizer> authList = new ArrayList<SecurityAuthorizer>();
    //How do I add StoreAuthorizer while maintaining a Singleton?
    //Will the line below do it through Guice magic?
    //authList.add(new StoreAuthorizer());
    return authList;
}

My question is embedded in the code comments. When I'm adding StoreAuthorizer to that List<SecurityAuthorizer>:

  • How do I ensure it's the same instance as other StoreAuthorizer references?
  • Is that something Guice is just doing under the hood, so new StoreAuthorizer() really is calling an impl of getInstance() behind the scenes?
like image 586
Snekse Avatar asked Mar 16 '11 18:03

Snekse


People also ask

What does @inject do in Guice?

Note that the only Guice-specific code in the above is the @Inject annotation. This annotation marks an injection point. Guice will attempt to reconcile the dependencies implied by the annotated constructor, method, or field.

What is @named annotation in Guice?

Dependency Injection for Java Developers with Dagger & Guice Guice provides another way also to map bindings without creating a custom annoation. It allows so using @Named annotation.

What is module in Guice?

Interface Module A module contributes configuration information, typically interface bindings, which will be used to create an Injector . A Guice-based application is ultimately composed of little more than a set of Module s and some bootstrapping code.

What is abstract module in Guice?

public abstract class AbstractModule extends java.lang.Object implements Module. A support class for Module s which reduces repetition and results in a more readable configuration. Simply extend this class, implement configure() , and call the inherited methods which mirror those found in Binder .


1 Answers

Provider methods allow injected arguments. The StoreAuthorizer passed to the method here will be the singleton bound in your module. Guice doesn't and can't do anything magical if you call a constructor yourself.

@Provides @CompositeSecurityAuthorizerAnnot
List<SecurityAuthorizer> provideAuthorizersList(StoreAuthorizer storeAuthorizer)
{
    List<SecurityAuthorizer> authList = new ArrayList<SecurityAuthorizer>();
    authList.add(storeAuthorizer);
    return authList;
}

As an aside, you may want to consider using the Guice Multibindings extension to create a Set<SecurityAuthorizer> rather than doing this yourself.

like image 178
ColinD Avatar answered Nov 15 '22 04:11

ColinD