Java 如何初始化云Firestore Bean以在所有包中使用?

Java 如何初始化云Firestore Bean以在所有包中使用?,java,reactjs,firebase,spring-boot,google-cloud-firestore,Java,Reactjs,Firebase,Spring Boot,Google Cloud Firestore,我尝试按照firebase google文档初始化管理员SDK- 这段代码就是我所指的 import com.google.auth.oauth2.GoogleCredentials; 导入com.google.cloud.firestore.firestore; 导入com.google.firebase.FirebaseApp; 导入com.google.firebase.FirebaseOptions; //使用服务帐户 InputStream serviceAccount=新文件Inpu

我尝试按照firebase google文档初始化管理员SDK-

这段代码就是我所指的

import com.google.auth.oauth2.GoogleCredentials;
导入com.google.cloud.firestore.firestore;
导入com.google.firebase.FirebaseApp;
导入com.google.firebase.FirebaseOptions;
//使用服务帐户
InputStream serviceAccount=新文件InputStream(“path/to/serviceCount.json”);
GoogleCredentials=GoogleCredentials.fromStream(serviceAccount);
FirebaseOptions=newfirebaseOptions.Builder()
.setCredentials(凭证)
.build();
FirebaseApp.initializeApp(选项);
Firestore db=FirestoreClient.getFirestore();
但我遇到了一些问题。他们的文档没有显示如何在整个应用程序中使用初始化的“Firestore db”变量。他们还使用“FirestoreClient”,为我在编译时抛出错误。我有一个带有存储库和DAO的用户类,我想用它来保持事物的结构化。我想将Firestore Bean自动连接到DAO类,以便使用查询我的云Firestore数据库

以下是启动应用程序的主要java类:

package com.nocton;
导入org.springframework.data.jpa.repository.config.EnableJpaRepositories;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入com.google.auth.oauth2.GoogleCredentials;
导入com.google.cloud.firestore.firestore;
导入com.google.firebase.FirebaseApp;
导入com.google.firebase.FirebaseOptions;
导入java.io.InputStream;
导入java.io.FileInputStream;
导入java.io.IOException;
@SpringBootApplication(scanBasePackages=“com.nighton.user”)
公共类RespondSpringDataRestapplication{
公共静态void main(字符串[]args){
run(reactionsSpringDataRestapplication.class,args);
试一试{
//使用服务帐户
InputStream serviceAccount=新文件InputStream(“../../../resources/static/serviceAccount.json”);
GoogleCredentials=GoogleCredentials.fromStream(serviceAccount);
FirebaseOptions=newfirebaseOptions.Builder()
.setCredentials(凭证)
.build();
FirebaseApp.initializeApp(选项);
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
这是我的UserDAO类:

package com.nighton.user;
导入com.nighton.user.user;
导入org.springframework.stereotype.Repository;
导入org.springframework.beans.factory.annotation.Autowired;
导入com.google.firebase.FirebaseApp;
导入com.google.api.core.ApiFuture;
导入com.google.cloud.firestore.DocumentReference;
导入com.google.cloud.firestore.DocumentSnapshot;
导入com.google.cloud.firestore.firestore;
@存储库
公共类UserDAO{
@自动连线
私人消防仓库数据库;
公共用户DAO(Firestore数据库){
这个.db=db;
}
/**
*将集合中的文档作为自定义对象检索。
*
*@将文档数据作为用户对象返回
*/
公共用户getDocumentSentity(){
//[启动fs\u获取\u文档作为\u实体]
试一试{
DocumentReference docRef=db.collection(“用户”).document(“41xfWB5B6josnRPYt55P”);
//异步检索文档
ApiFuture=docRef.get();
//阻塞响应
DocumentSnapshot document=future.get();
User=null;
if(document.exists()){
//将文档转换为POJO
user=document.toObject(user.class);
System.out.println(用户);
}否则{
System.out.println(“没有这样的文档!”);
}
//[结束fs\u获取文档作为\u实体]
返回用户;
}捕获(例外e){
系统输出打印ln(e);
返回null;
}
}
}
很明显,我在自动连接Firestore db时会出错,因为没有为其创建可自动连接的bean。我尝试创建一个bean来返回主类中的Firestore变量,但是FirestoreClient不断抛出错误。我不确定该如何设置这个

我尝试将此添加到我的主类中,但仍然出现一些错误:

@Bean
公共静态Firestore initFirebaseFirestore(){
试一试{
//使用服务帐户
InputStream serviceAccount=新文件InputStream(“../../../resources/static/serviceAccount.json”);
GoogleCredentials=GoogleCredentials.fromStream(serviceAccount);
FirebaseOptions=newfirebaseOptions.Builder()
.setCredentials(凭证)
.build();
FirebaseApp.initializeApp(选项);
Firestore db=FirestoreClient.getFirestore();
返回分贝;
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
}
当我不创建bean时: 说明:

com.nocton.user.UserDAO中构造函数的参数0需要找不到类型为“com.google.cloud.firestore.firestore”的bean

注入点具有以下注释: -@org.springframework.beans.factory.annotation.Autowired(必需=true)

行动:

考虑在配置中定义“com.google.cloud.firestore.firestore”类型的bean

将bean添加到主类时: 编译失败 [错误]/Users/tristanheilman/Documents/MapApp/src/main/java/com/nocton/reactedspringdatarestapplication.java:[37,40]找不到符号 [错误]符号:变量FirestoreClient
[错误]位置:class com.nighton.reactedspringdatarestapplication

我能够解决我的答案。主要原因是进口不正确:

import com.google.firebase.cloud.FirestoreClient;
是必要的,然后我也错过了一些错误