Use @Inject inside the module class

90 views
Skip to first unread message

vishesh kamdar

unread,
Aug 26, 2016, 4:51:19 PM8/26/16
to google-guice

So I'm trying to do something like this inside the Module class


@Inject(optional = true)
@Named("TotalQuantity")
private int totalQuantity = 5


Is this the correct way to set the default value, if the dependency is not provided?

vishesh kamdar

unread,
Aug 26, 2016, 6:40:34 PM8/26/16
to google-guice

As far as I can understand, use @Autowired inside a Spring config but cannot use @Inject in a Guice Module file. Is this true? (JAVA)

I want to use something like this inside the Module class

public class MyModule extends AbstractModule 
{

  @Inject(optional = true)
  @Named("TotalQuantity")

  private int totalQuantity = 5;

  @Provides
  @Singleton
  Boolean getBoolean()
  {
    if(totalQuantity>10)
      return true;
    else
      return false;
  }

  @Override
  protected void configure() {}
}

Is this the correct way to set the default value, if the dependency is not provided?

Marshall Pierce

unread,
Aug 27, 2016, 4:20:18 PM8/27/16
to google...@googlegroups.com
You probably want to use a Provider (which can take injected constructor params) or a @Provides method (which can take injected method params) if you want to have Guice-injected types as inputs to your module logic.

> On Aug 26, 2016, at 3:40 PM, vishesh kamdar <vishesh...@gmail.com> wrote:
>
> down vote
> favorite
> --
> You received this message because you are subscribed to the Google Groups "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
> To post to this group, send email to google...@googlegroups.com.
> Visit this group at https://groups.google.com/group/google-guice.
> To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/1e869cb7-478a-4b08-abf8-b3e6853bbb16%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

vishesh kamdar

unread,
Aug 28, 2016, 4:09:21 AM8/28/16
to google-guice
Yes, that was the approach I used. Used a Provider Binding. Is there a better way to handle this?
Reply all
Reply to author
Forward
0 new messages