How to use Guava ServiceManager with Guice Injection

五迷三道 提交于 2019-12-04 09:12:46

In my opinion, setting the services to be @Singleton isn't a hack at all. That's probably what I'd do.

@Provides @Singleton
public MyService myService() {
  return new MyService();
}

@Provides
public Set<Service> services(MyService myService) {
  return ImmutableSet.<Service>of(myService);
}

Then you can just inject any particular service instance you want anywhere you want.

ServiceManager.getServicesByState().get(RUNNING) returns the running services and ServiceManager.getServicesByState().values() returns all of the services managed by the ServiceManager.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!