Tôi đang thử lần đầu tiên và sau khi định cấu hình mô-đun, tôi gặp phải lỗi mà tôi không hiểu.Dagger: lớp học không thể bị ràng buộc với khóa
Mã của tôi là:
public class Parrot {
private Language language;
@Inject
public Parrot(Language language) {
this.language = language;
}
public void sayHello() {
System.out.println(language.getSalute());
}
}
public interface Language {
public String getSalute();
}
public class EnglishLanguage implements Language {
@Override
public String getSalute() {
return "Hello!";
}
}
mô-đun của tôi là
@Module(
injects = Language.class
)
public class PetShopModule {
@Provides Parrot provideParrot(Parrot parrot){
return parrot;
}
}
Và trong đoạn code tôi sử dụng nó theo cách này
EnglishLanguage lang=SystemApp.getSystemLanguage();
ObjectGraph objectGraph = ObjectGraph.create(new PetShopModule());
objectGraph.inject(myLanguage);
Parrot parrot = objectGraph.get(Parrot.class);
Trình biên dịch phàn nàn với:
error: com.example.petshop.Language could not be bound
with key com.example.petshop.Language required by com.example.petshop.PetShopModule
for com.example.petshop.PetShopModule
Tôi làm gì sai?
Lưu ý: Đây chỉ là một ví dụ đơn giản, trong đoạn code thực sự EnglishLanguage là một lớp hệ thống, và tôi không thể thay đổi cũng không tạo ra nó, chỉ cần có được một tài liệu tham khảo