I want do a binding like this,
bind(Supplier<TestClass>).toProvider(Provider<Supplier<TestClass>>).in(Singleton.class);
The provider is returned by an external function so, inside the toProvider()
, I call that function and it returns Provider<Supplier<TestClass>>
.
The supplier is from guava, the reason to do this kind of thing is, there is a file associated with the TestClass and I need to read that file and assign those values to the respective fields of TestClass.
And that file change at run time, So I need a way to refresh the values stored in TestClass. To-Do that guava supplier I used. Guava supplier has a get method and when that get method is called, if I used memoizeWithExpiration()
to create the instance then it checks the TTL value and if it passed, then I can specify a lambda function to read the file and assign values.
So I need to inject Supplier<TestClass>
like this
@Inject
Supplier<TestClass> someClassSupplier;
But For doing that binding with Guice is confusing for me.
You can use the following kind of code to do what you want:
Then, in your class:
You requested for a generic way of doing this, so here it is, check the
bindSupplier
method: