我想将MyImpl绑定到Multibinding。但是MyImpl的构造函数带有参数。
var currentUserChats = [Chat]() {
didSet(newValue){
attachChildChangedObserverOn(chat: newValue)
}
}
var observersArray = [String: UInt]() // chatUID:handle
func attachChildChangedObserverOn(chat: Chat) {
var handle: UInt = 0
let ref = DDatabaseRReference.chats.reference().child(chat.chatUID).child("users").child(currentUser.userUID)
handle = ref.observe(.childChanged, with: {[weak self] (snapshot) in
self?.observersArray[chat.chatUID] = handle
print("snapshot.value is \(snapshot.value) and snapKey is \(snapshot.key)")
guard snapshot.exists() else {return}
let chatChanged = chat
var lastMessage = ""
var unreadMessagesCount = 0
var lastUpdate = 0.0
switch snapshot.key {
//case....
}
})
}
我不想@Inject它,因为它是布尔值,可以偶尔将其注入其他地方。所以。我可以介绍一些Enum并注入它,然后怎么做呢?或者我可以更好地写
final Multibinder<MyInterface> binder = Multibinder.newSetBinder(binder(), MyInterface.class)
binder.addBinding().to(MyImpl.class);
public MyImpl(Boolean myParam) ...
等等?
答案 0 :(得分:1)
我不想@Inject它,因为它是布尔值,可以偶尔将其注入其他地方。 为避免这种情况,请使用“命名注释”。
解决方案一:
@Inject
public TextEditor(@Named("OpenOffice") SpellChecker spellChecker) { ...}
这是绑定代码:
bind(SpellChecker.class).annotatedWith(Names.named("OpenOffice")).to(OpenOfficeWordSpellCheckerImpl.class);
解决方案二:
在模块中加载java-properties并使用java-prop-names:
private static Properties loadProperties(String name){
Properties properties = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream is = loader.getResourceAsStream(name);
try {
properties.load(is);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if(is != null){
try {
is.close();
} catch (IOException dontCare) { }
}
}
return properties;
}
protected void configure() {
try{
Properties gameProperties = loadProperties("game.properties");
Names.bindProperties(binder(),gameProperties);
}catch (RuntimeException ex){
addError("Could not configure Game Properties");
};
}