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();
//...
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top