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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:13:51+00:00 2026-06-09T10:13:51+00:00

I’m currently trying to use Google Guice-3.0 in a small application. When running this

  • 0

I’m currently trying to use Google Guice-3.0 in a small application.

When running this app, the user is prompted to input his name and his password. Since this information is not known until runtime, I use AssistedInject to implement my User instantiation.

This is where my UserFactory comes in. It provides a User with the method

public User create(@Assisted("Username") String username, 
                   @Assisted("Password") String password);

The User class is initiated once at the start of the program (after the user input) via

User user = getInjector().getInstance(UserFactory.class).create(username, password);

and I want to use this instance over the lifetime of the application. I have already set the scope to @Singleton.

The problem is, however, I only get errors. Guice is complaining that I haven’t passed any variables when calling

User user = getInjector().getInstance( User.class );

and if I add a bind( User.class ); into the configure method, an error occurs that there was no declaration to the annotation @Assisted (since you can put annotations in front of parameters to uniquely identify them – Guice probably thinks it’s one of them and demands a dependency to be set (like

bind( String.class ).annotatedWith( Assisted.class ).toInstance( username );

but this is not a thing that would work (maybe it would via static references but then why use Guice?

Here is an example that you can compile. The commented code causes the error. Especially the last 2 lines are pretty irritating to me.

public class KSKB {

    @Singleton
    public static class User {

        public final String name;

        @Inject
        public User(@Assisted("Username") String username) {
            this.name = username;
        }

        public static interface Factory {
            public User create(@Assisted("Username") String username);
        }
    }

    public static void main(String... args) {
        Injector injector = Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                install(new FactoryModuleBuilder().build(User.Factory.class));
                // bind( User.class );
            }

        });
        User user = injector.getInstance(User.Factory.class).create("Guice");
        System.out.println(user.name);

        // user = injector.getInstance( User.class ); // doesn't work - throws Exception!!
        // System.out.println( user.name );
    }
}
  • 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-09T10:13:52+00:00Added an answer on June 9, 2026 at 10:13 am

    I don’t think can get what you want because you want to spoil Guice.

    Guice wants to manages instances and their creation by itself. You can use Provider and custom factories like the Assisted stuff to create object on your own – even with support from Guice. But you cannot feed these instances back into Guice. This means, you cannot tell Guice to use a specific object as the singleton instance.1

    Possible and easy solutions:

    • Determine the user name before creating the injector and use bindConstant() in the module to bind the user name. Use the appropriate counterpart in User. Then you can mark User with @Singleton and Guice will honour it.

    • User bind(User.class).toInstance(myUser);. This is similar to the first proposal, because the required username must be fetched before Guice is up and running.

    • Do not inject User but an intermediate object UserHolder (or UserManager) which is the singleton. This object has methods to store the actual user and to fetch it. Your initialisation code will fetch that holder (which is empty at that time) and put the created User into the holder. Other parts of your application will also inject UserHolder.


    1 without dirty tricks at least.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I am currently running into a problem where an element is coming back from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small

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.