使用guice绑定Guava供应商

mkshixfv  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(431)

我想做这样的装订,

  1. bind(Supplier<TestClass>).toProvider(Provider<Supplier<TestClass>>).in(Singleton.class);

提供程序由外部函数返回,因此 toProvider() ,我调用该函数,它返回provider <Supplier<TestClass>> .
供应商来自guava,这样做的原因是,有一个与testclass相关联的文件,我需要读取该文件并将这些值赋给testclass的相应字段。
文件在运行时会发生更改,所以我需要一种方法来刷新testclass中存储的值。做我用过的Guava供应商。guava供应商有一个get方法,当调用该get方法时,如果我使用 memoizeWithExpiration() 为了创建示例,它检查ttl值,如果传递了,那么我可以指定lambda函数来读取文件并赋值。
所以我需要注射 Supplier<TestClass> 这样地

  1. @Inject
  2. Supplier<TestClass> someClassSupplier;

但是用guice绑定对我来说很混乱。

tktrz96b

tktrz96b1#

可以使用以下类型的代码执行所需操作:

  1. class ServiceModule extends AbstractModule {
  2. private TestClass readTestClassFromFile() {
  3. return new TestClass();
  4. }
  5. // Cache an instance for 5 seconds.
  6. private final Supplier<TestClass> testClassSupplier = Suppliers.memoizeWithExpiration(this::readTestClassFromFile, 5, SECONDS);
  7. @Provides TestClass provideTestClass() { // Don't declare as singleton
  8. return testClassSupplier.get();
  9. }
  10. }

然后,在你的课堂上:

  1. class Service {
  2. @Inject
  3. Provider<TestClass> testClassProvider; // Inject the provider, not the instance itself, or any supplier.
  4. void doSomething() throws Exception {
  5. TestClass a = testClassProvider.get();
  6. TestClass b = testClassProvider.get();
  7. Thread.sleep(6000); // Sleep for 6 seconds
  8. TestClass c = testClassProvider.get();
  9. System.out.println(a == b); // Prints true
  10. System.out.println(a == c); // Prints false
  11. }
  12. }

你要求一个通用的方法来做这件事,所以在这里,检查 bindSupplier 方法:

  1. import static com.google.common.base.Suppliers.memoizeWithExpiration;
  2. import com.google.inject.*;
  3. import java.util.concurrent.TimeUnit;
  4. import java.util.function.Supplier;
  5. public class Main {
  6. public static void main(String[] args) throws Exception {
  7. Guice.createInjector(new ServiceModule())
  8. .getInstance(Service.class)
  9. .doSomething();
  10. }
  11. static class ServiceModule extends AbstractModule {
  12. Dependency createDependency() { return new Dependency(); }
  13. // For Java 8+
  14. private <T> void bindSupplier(Class<T> type, Supplier<? extends T> supplier) {
  15. // Definitely avoid .in(Singleton.class) because you want the scope to be defined by the Supplier.
  16. bind(type).toProvider(supplier::get);
  17. }
  18. // For Java 7 and less
  19. // private <T> void bindSupplier(Class<T> type, final Supplier<? extends T> supplier) {
  20. // bind(type).toProvider(new Provider<T>() {
  21. // @Override public T get() { return supplier.get(); }
  22. // });
  23. // }
  24. @Override protected void configure() {
  25. bindSupplier(Dependency.class,
  26. memoizeWithExpiration(this::createDependency, 3, TimeUnit.SECONDS));
  27. }
  28. }
  29. static class Dependency {}
  30. static class Service {
  31. @Inject Provider<Dependency> dependencyProvider;
  32. void doSomething() throws InterruptedException {
  33. Dependency a = dependencyProvider.get();
  34. Dependency b = dependencyProvider.get();
  35. Thread.sleep(4000);
  36. Dependency c = dependencyProvider.get();
  37. System.out.printf("a == b ? %s%n", a == b); // true
  38. System.out.printf("a == c ? %s%n", a == c); // false
  39. }
  40. }
  41. }
展开查看全部

相关问题