Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9258319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:22:12+00:00 2026-06-18T12:22:12+00:00

In Guice, what’s the difference between: // Inside your AbstractModule subclass: @Override public void

  • 0

In Guice, what’s the difference between:

// Inside your AbstractModule subclass:
@Override
public void configure() {
    bind(Service.class).to(ServiceImpl.class).in(Singleton.class);
}

And:

@Override
public void configure() {
    bind(Service.class).to(ServiceImpl.class);
}

@Provides @Singleton
public ServiceImpl providesService() {
    return new ServiceImpl();
}

Are they both the same? When would you use one versus the other? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:22:13+00:00Added an answer on June 18, 2026 at 12:22 pm

    They are nearly identical. The @Singleton syntax is useful for annotating @Provides methods, or annotating the class itself (though I prefer to keep my scoping annotations inside modules).

    The difference lies in which key is marked Singleton, which has less to do with @Singleton versus Singleton.class (or Scopes.SINGLETON, asEagerSingleton, @Singleton class annotations, or toInstance implicit singletons) and more to do with what the default syntax makes easy. For example:

    public class MyModule extends AbstractModule {
      @Override public void configure() {
        bind(A.class).to(AImpl.class).in(Singleton.class);
    
        bind(B.class).to(BImpl.class);
        bind(BImpl.class).in(Singleton.class);
      }
    
      @Provides @Singleton C provideC() { return new CImpl(); }
    
      @Provides @Singleton D provideD(DImpl dImpl) { return dImpl; }
    
      @Provides E provideE(EImpl eImpl) { return eImpl; }
      @Provides @Singleton EImpl provideEImpl() { return new EImpl(); }
    }
    

    Above we’ve bound interface A to class AImpl, and interface B to class BImpl, but the behavior is different:

    • Injecting A will retrieve the same AImpl instance every time.
    • Injecting AImpl will retrieve a different AImpl every time, all of which are different than A‘s instance.
    • Injecting B will retrieve the same BImpl instance every time.
    • Injecting BImpl will also retrieve that same BImpl instance that B injects.

    As you can see, each key is different, and Guice will allow multiple implementation instances if only the interface is bound with Singleton. If you only ever inject A and B interfaces, the behavior looks identical, but if you inject both interfaces and implementations from the same Injector, you may see differing behavior.

    Similar logic goes for @Provides methods:

    • Injecting C will always return the same CImpl instance.
    • Injecting CImpl will create a new CImpl every time, unless CImpl has no injectable public zero-arg constructor—then the injection will fail.
    • Injecting D will always return the same DImpl instance.
    • Injecting DImpl will return a new instance every time, and each will be different than the one returned by D.
    • Injecting E will return the same EImpl instance every time.
    • Injecting EImpl will also retrieve that same instance E injects.

    This provides some flexibility. Imagine a hypothetical Cache that keeps a certain number of most-recently-retrieved objects, where you want to have @User Cache and @Product Cache both injectable. If you bind(Cache.class).in(Singleton.class), you will have one Cache shared between the objects (and any bare Cache injections), whereas if you bind(Cache.class).annotatedWith(User.class).to(Cache.class).in(Singleton.class) then the annotated key is kept in singleton scope and each object type will have its own cache.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Guice configuration looks something like: public class SocialServiceGuiceConfig extends GuiceServletContextListener { @Override protected
Using Guice, if I have @Inject @Named(light) Color light; I can use bind(Color.class).annotatedWith(Names.named(light)) .toInstance(new
In Guice's documentation, the chapter about Untargetted Bindings presents this example: bind(AnotherConcreteClass.class).in(Singleton.class); What does
I create guice servlet like this: public class GuiceApplicationServlet extends AbstractApplicationServlet { protected Provider<Application>
In the Guice documentation, there is an Untargetted Binding example as: bind(MyConcreteClass.class) .annotatedWith(Names.named(foo)) .to(MyConcreteClass.class);
We are using GUICE framework as part of service layer. Now there is a
Guice Singletons are weird for me First I thought that IService ser = Guice.createInjector().getInstance(IService.class);
With reference to Guice's custom injections article, its TypeListener performs a check for InjectLogger.class
I'm using guice to dynamically inject classes in my constructors. For example: @Inject public
I've configured my servlets/filters using guice-servlet. With bindings like serve(/foo).with(HelloServlet.class); Now, I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.