Вопрос

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