2

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 );
    }
}
4

2 に答える 2

3

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.

于 2012-08-07T08:44:03.937 に答える
0

The output of assisted injection is not injection - it's auto-wired factories (the facility is horribly named, sadly).

What gets produced by AssistedInject is a Factory - that Factory can be singleton. But it's a factory, so it's job is to create objects, like the User object. The internals of the Factory aren't set up to let it create Singleton value objects. Value objects are, generally speaking, not singleton objects - you're using one here as "Context" rather than as data, and so you have a special case.

Also, your case is even more special because you want to pre-validate the data in the database before guice is even running. This user is entirely context, not data, so don't use a factory to create it. Create it manually, and if you need to use it elsewhere in guice-bound systems, then create it outside Guice, and pass it into a module as a DatabaseCredentials object or some such.

To properly help you we'd need to see a larger design concept, but my sense is, you're not thinking clearly about the difference between collaborators/services, value-types, context/configuration, etc. and that the confusion is largely coming because the word "User" is overloaded here. Yes, it's a "user" but the database user isn't the same kind of user as other Users in the system - even if it is structurally identical, it means something different, has a different lifecycle, etc.

于 2012-08-20T14:15:25.827 に答える