简体   繁体   中英

Google-guice: bind all instances of the same class to certain instance

I do not have a problem to bind singleton instance to some other instance using @inject. But, when it comes to binding more instances of one class to some other instance (which is singletone) it refuses to bind it somehow and I'm getting null reference.

My code example is:

public class WebSocketManagerImpl implements WebSocketManager {
@Inject
private FactoryWebSocket factoryImpl;
}

When I create several instances of WebSocketManagerImpl, every factoryImpl field is null pointer. Am I doing something wrong?

Please use Binding Annotations ( https://code.google.com/p/google-guice/wiki/BindingAnnotations ). This way you can annotate multiple instances of same class, and then have them selectively injected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM