Domanda

The Spring lookup method lets you have a method that returns a new instance of an object every time you call it. I read the Guice user guide and didn't see an obvious way of how to do this. I'd like to have some code like this:

@Inject
private FooInstanceFactory fooInstanceFactory;

//...
    Foo foo = fooInstanceFactory.getNewInstanceOfFoo();
//...
È stato utile?

Soluzione

This really isn't the Guice way of doing things. You can inject a Provider<Foo> and call Provider#get() on it, but the calling code shouldn't be concerned with whether or not you're getting a new instance each time. That's for the module's configuration to worry about.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top