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

  • SEARCH
  • Home
  • 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 3304242
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:02:28+00:00 2026-05-17T21:02:28+00:00

There’s something I just don’t get about guice: According to what I’ve read so

  • 0

There’s something I just don’t get about guice: According to what I’ve read so far, I’m supposed to use the Injector only in my bootstrapping class (in a standalone application this would typically be in the main() method), like in the example below (taken from the guice documentation):

public static void main(String[] args) {
    /*
     * Guice.createInjector() takes your Modules, and returns a new Injector
     * instance. Most applications will call this method exactly once, in their
     * main() method.
     */
    Injector injector = Guice.createInjector(new BillingModule());

    /*
     * Now that we've got the injector, we can build objects.
     */
    RealBillingService billingService = injector.getInstance(RealBillingService.class);
    ...
  }

But what if not all Objects I ever need can be created during startup? Maybe I want to respond to some user interaction when the application is running? Don’t I have to keep my injector around somewhere (e.g. as a static variable) and then call injector.getInstance(SomeInterface.class) when I need to create a new object?

Of course spreading calls to Injector.getInstance() all over the place seems not to be desirable.

What am I getting wrong here?

  • 1 1 Answer
  • 2 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-05-17T21:02:29+00:00Added an answer on May 17, 2026 at 9:02 pm

    Yes, you basically only should use the Injector to create get the instance for the root-object. The rest of the application shouldn’t touch the Guice-Container. As you’ve noticed, you still need to create some objects when required. There are different approaches for doing that, each suitable for different needs.

    Inject a Provider
    Provider is a interface from Guice. It allows you to request a new instance of a object. That object will be created using Guice. For example.

     class MyService{
         private Provider<Transaction> transactionProvider;
         public MainGui(Provider<Transaction> transactionProvider){
             this.transactionProvider = transactionProvider;
         }
    
         public void actionStarted(){
             Transaction transaction = transactionProvider.get();
         }
    

    Build a Factory
    Often you need some kind of factory. This factory uses some injected services and some parameters and creates a new object for you. Then you use this factory for new instances. Then you inject that factory and use it. There also help for this with the AssistedInject-extension

    I think with these two possibilities you rarely need to use the Guice-Injector itself. However sometimes is still appropriate to use the injector itself. Then you can inject the Injector to a component.

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

Sidebar

Related Questions

There's a Rails 3.2.3 web application which doesn't use any database. But in spite
There are nice SO question and answers about this issue, but these options didn't
There was a similar question posted asking in general about alternatives to Qt. Well,
There's a few previous questions on StackOverflow questioning how one goes about accessing local
I want use html5's new tag to play a wav file (currently only supported
There are a lot of questions about full-joining in mysql(5.1.36). Of course the solution
There are many tutorials that talk about deleting index.php from the url. But I
There are a few ways to get class-like behavior in javascript, the most common
There is something strange happening. Param interceptor is the one in charge to take
There is something I miss with the notion of Synchronizing code in Android. Scenario

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.