java Spring Boot“名为[DEFAULT]的FirebaseApp不存在。”
启动Spring Boot's jar file
会给我带来以下错误:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'temperatureController' defined in URL <...>
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'temperatureService' defined in URL <...>
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.temperaturetracker.services.TokenService]: Constructor threw exception; nested exception is java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.
每个类都包含适当的@
:@Service
或@RestController
或@SpringBootApplication
或@Entity
或@Repository
一些课程:
@Service
public class TemperatureService {
private final AlertService alertService;
@Autowired
public TemperatureService(AlertService alertService) {
this.alertService = alertService;
}
<...>
}
@Service
class AlertService @Autowired constructor(private val tokenService: TokenService,
private val cloudMessagingService: CloudMessagingService) {
@PostConstruct
fun initialize() {
<...>
}
}
@Service
public class CloudMessagingService {
final Logger logger = LoggerFactory.getLogger(CloudMessagingService.class);
public void sendFirebaseMessage() {
<...>
try {
var response = FirebaseMessaging.getInstance().send(fbMessage);
logger.debug("Notification response: " + response);
} catch (FirebaseMessagingException e) {
e.printStackTrace();
logger.error("Error sending Firebase Cloud Message: " + e);
}
}
}
@Service
public class FirebaseInitialize {
@PostConstruct
public void initialize() {
try {
FileInputStream serviceAccount =
new FileInputStream("hidden-path");
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("hidden-path")
.build();
FirebaseApp.initializeApp(options);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@SpringBootApplication
public class TemperatureTrackerApplication {
public static void main(String[] args) {
SpringApplication.run(TemperatureTrackerApplication.class, args);
}
}
这些错误只有在启动jar
文件时才会发生。通过绿色箭头或Shift+F10运行应用程序,一切都能完美运行
# 1 楼答案
我已将类的
FirebaseInitialize
方法initialize()
更改为:FileInputStream
我以前使用过,希望资源位于文件系统上,不能嵌套在jar
文件中。因此,使用getInputStream()
的ClassPathResource
有效请阅读更多:Classpath resource not found when running as jar
# 2 楼答案
确保Firebase配置正常,因为在SpringBoot尝试执行该类时会引发错误