![](/img/trans.png)
[英]How to use a single Firestore instance throughout an android app?
[英]How to initialize Cloud Firestore Bean to use throughout all packages?
我嘗試按照 firebase google 文檔來初始化 Admin SDK - https://firebase.google.com/docs/firestore/quickstart
這部分代碼就是我所指的
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.Firestore;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
// Use a service account
InputStream serviceAccount = new FileInputStream("path/to/serviceAccount.json");
GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(credentials)
.build();
FirebaseApp.initializeApp(options);
Firestore db = FirestoreClient.getFirestore();
但是我遇到了一些問題。 他們的文檔沒有展示如何在整個應用程序中使用初始化的“Firestore db”變量。 他們還使用“FirestoreClient”,它會為我拋出編譯錯誤。 我有一個帶有存儲庫和 DAO 的 User 類,我想用它來保持結構化。 我想將 Firestore Bean 自動裝配到 DAO 類,以便我可以使用查詢我的雲 Firestore 數據庫。
這是我啟動應用程序的主要 Java 類:
package com.nocturn;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.Firestore;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
@SpringBootApplication(scanBasePackages = "com.nocturn.user")
public class ReactAndSpringDataRestApplication {
public static void main(String[] args) {
SpringApplication.run(ReactAndSpringDataRestApplication.class, args);
try {
// Use a service account
InputStream serviceAccount = new FileInputStream("../../../resources/static/serviceAccount.json");
GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(credentials)
.build();
FirebaseApp.initializeApp(options);
} catch (IOException e) {
e.printStackTrace();
}
}
}
這是我的 UserDAO 類:
package com.nocturn.user;
import com.nocturn.user.User;
import org.springframework.stereotype.Repository;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.firebase.FirebaseApp;
import com.google.api.core.ApiFuture;
import com.google.cloud.firestore.DocumentReference;
import com.google.cloud.firestore.DocumentSnapshot;
import com.google.cloud.firestore.Firestore;
@Repository
public class UserDAO {
@Autowired
private final Firestore db;
public UserDAO(Firestore db) {
this.db = db;
}
/**
* Retrieves document in collection as a custom object.
*
* @return document data as User object
*/
public User getDocumentAsEntity() {
// [START fs_get_doc_as_entity]
try {
DocumentReference docRef = db.collection("users").document("41xfWB5B6josnRPYt55P");
// asynchronously retrieve the document
ApiFuture<DocumentSnapshot> future = docRef.get();
// block on response
DocumentSnapshot document = future.get();
User user = null;
if (document.exists()) {
// convert document to POJO
user = document.toObject(User.class);
System.out.println(user);
} else {
System.out.println("No such document!");
}
// [END fs_get_doc_as_entity]
return user;
} catch(Exception e) {
System.out.println(e);
return null;
}
}
}
我顯然會收到有關 Autowiring the Firestore db 的錯誤,因為沒有為它創建自動裝配的 bean。 我嘗試創建一個 bean 以在我的主類中返回 Firestore 變量,但 FirestoreClient 不斷拋出錯誤。 我不確定我應該如何設置。
我嘗試將它添加到我的主類中,但仍然有一些錯誤:
@Bean
public static Firestore initFirebaseFirestore() {
try {
// Use a service account
InputStream serviceAccount = new FileInputStream("../../../resources/static/serviceAccount.json");
GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(credentials)
.build();
FirebaseApp.initializeApp(options);
Firestore db = FirestoreClient.getFirestore();
return db;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
當我不創建 bean 時:描述:
com.nocturn.user.UserDAO 中構造函數的參數 0 需要一個無法找到的類型為“com.google.cloud.firestore.Firestore”的 bean。
注入點有以下注釋: - @org.springframework.beans.factory.annotation.Autowired(required=true)
行動:
考慮在您的配置中定義一個“com.google.cloud.firestore.Firestore”類型的 bean。
當我將 bean 添加到我的主類時:編譯失敗 [ERROR] /Users/tristanheilman/Documents/MapApp/src/main/java/com/nocturn/ReactAndSpringDataRestApplication.java:[37,40] 找不到符號 [ERROR] 符號:變量 FirestoreClient [錯誤] 位置:類 com.nocturn.ReactAndSpringDataRestApplication
我能夠解決我的答案。 主要原因是不正確的導入:
import com.google.firebase.cloud.FirestoreClient;
是需要的,然后我也遺漏了一些錯誤,這些錯誤表明無法創建我的 serviceAccount.json 文件,因此 Firestore bean 為空。 我解決了這個問題,應用程序啟動,我可以使用 curl 通過我創建的 API 來訪問我的雲 Firestore 數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.