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 824131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:03:18+00:00 2026-05-15T03:03:18+00:00

Guice Singletons are weird for me First I thought that IService ser = Guice.createInjector().getInstance(IService.class);

  • 0

Guice Singletons are weird for me

First I thought that

IService ser = Guice.createInjector().getInstance(IService.class);
System.out.println("ser=" + ser);
ser = Guice.createInjector().getInstance(IService.class);
System.out.println("ser=" + ser);

will work as singleton, but it returns

ser=Service2@1975b59
ser=Service2@1f934ad

its ok, it doesnt have to be easy.

Injector injector = Guice.createInjector();
IService ser = injector.getInstance(IService.class);
System.out.println("ser=" + ser);
ser = injector.getInstance(IService.class);
System.out.println("ser=" + ser);

works as singleton

ser=Service2@1975b59
ser=Service2@1975b59

So i need to have static field with Injector(Singleton for Singletons)

how do i pass to it Module for testing?

  • 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-05-15T03:03:19+00:00Added an answer on May 15, 2026 at 3:03 am

    When using Guice, you should create exactly one injector per JVM. Usually you’ll create it in your application’s entry point (ie. in your public static void main method). If you call createInjector multiple times in your application, you may be using Guice wrong!

    Singletons are instance-per-injector. Note that this is not instance-per-JVM. The best way to obtain a reference to a singleton is to be injected with it. The other way is to ask the one and only injector instance to give it to you.

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

Sidebar

Related Questions

(x-posted to guice mailing list) I'm trialling Guice on a new library that will
It seems that all Guice's out-of-the-box Scope implementations are inherently Thread-based (or ignore Threads
I met a problem when trying @Singleton of Guice: import com.google.inject.Singleton; @Singleton public class
I have a class like so, that I create myself somewhere in my code:
I have unit test, i use guice for di, i annotate my class with
I have an UI component that will be used in several UI module in
I have been told that Singletons are hard to test. http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/ http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/ I have
I create guice servlet like this: public class GuiceApplicationServlet extends AbstractApplicationServlet { protected Provider<Application>
I have a guice based app that now needs multiple instances of a given
I tried Google Guice the first time and find it very nice. But, when

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.