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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:29:52+00:00 2026-06-05T00:29:52+00:00

I am working on a project using Depedency injection Google Guice Framework. It’s possible

  • 0

I am working on a project using Depedency injection Google Guice Framework.

It’s possible to bind a class with the singleton scope, in Guice as:

bind(Foo.class).to(FooImpl.class).in(Scopes.SINGLETON);

If FooImpl class is itself a real singleton class as:

public class FooImpl implements Foo{
    public static final FooImpl INSTANCE = new FooImpl();
    public static FooImpl getInstance(){...}
    // ...
}

or

public class FooImpl implements Foo{
    private FooImpl instance = null;

    private FooImpl(){...}
    public static FooImpl getInstance(){
         if(instance == null)
             instance = new FooImpl();
         return FooImpl();
         // ...
    }

    // ...
}

So, It’s could be possible to declare two singletons in the project, the first one declared by Guice and the second one by the getInstance() traditionnal way.

Google Guice can also bind Interfaces to particular instance with the toInstance() method.

So, instead binding with the Singleton scope, shouln’t be a better way to bind Singleton, in Java with this declaration:

bind(Foo.class).toInstance(FooImpl.getInstance());

instead of the first one? Is it more secure? Is it possible to have two singletons instances in that way?

What’s the best way for declaring singletons with Google Guice?

  • 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-05T00:29:53+00:00Added an answer on June 5, 2026 at 12:29 am

    The first approach you suggest is preferred:

    bind(Foo.class).to(FooImpl.class).in(Scopes.SINGLETON);
    

    This allows Guice to perform dependency injection in FooImpl as needed. This is not possible when manually binding to an instance using toInstance() as in your last suggestion.

    Finally, the Singleton design pattern is an anti-pattern for many reasons. Note that this refers to the implementation using getInstance() and so on; having a single instance of a class for the lifetime of your app (like objects in Guice’s SINGLETON scope) is not bad by any means.

    Also see Guice’s page on scopes for some usage guidelines of the singleton scope.

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

Sidebar

Related Questions

I am working on a game project using Microsoft XNA framework, although this question
I'm working on a project using JSF 1.2 deploying a portlet to WebSphere Portal
I am working on a project using PlayFramework 2.0. After reading a bit of
I'm working on a project using JBoss 6.1.0.Final, JSF 2.0.3, EJB3 and JPA 2
I am working on a project using Groovy, and I would like to take
I'm working on a project using Scala running Selenium tests as part of a
My team working in a project using Winforms application(c#) & MSSQL 2005 as database.
I am working on a project using the Orb feature detector in OpenCV 2.3.1
I have been working on a project using AMFPHP and flash. I did the
I'm currently working on a project using netbeans 6.9 and grails 1.3. I'm trying

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.