Dependency injection 如果只注入直接依赖项,如何防止GoogleGuice中的循环引用
GoogleGuice中的最佳实践是。 但是如果我使用下面的示例代码,如何在Customer类中创建account的实例Dependency injection 如果只注入直接依赖项,如何防止GoogleGuice中的循环引用,dependency-injection,guice,circular-reference,Dependency Injection,Guice,Circular Reference,GoogleGuice中的最佳实践是。 但是如果我使用下面的示例代码,如何在Customer类中创建account的实例 问题是,Guice总是试图通过调用providePurchasingAccount()来获取新帐户,这会导致循环引用。您摘录的方法在CustomerModule类中,而不是Customer类中 这里有更多的细节 这取决于如何约束客户 在Guice wiki页面中,您提到它没有显示客户的绑定 假设模块也有此方法 @Provides Customer getCustomer(Da
问题是,Guice总是试图通过调用providePurchasingAccount()来获取新帐户,这会导致循环引用。您摘录的方法在CustomerModule类中,而不是Customer类中 这里有更多的细节 这取决于如何约束客户 在Guice wiki页面中,您提到它没有显示客户的绑定 假设模块也有此方法
@Provides
Customer getCustomer(Database database, long customerId) {
return database.getCustomer(customerId);
}
在这种情况下,当首先注入Account时,将调用此方法以获取Customer对象,然后调用providePurchasingAccount以获取Account。您将看到一个循环引用,其中没有循环引用。要提供帐户,您需要客户。但是没有,你在哪里看到,要向客户提供你需要的帐户。
@Provides
Customer getCustomer(Database database, long customerId) {
return database.getCustomer(customerId);
}