【发布时间】:2021-11-13 05:14:02
【问题描述】:
我正在用 Kotlin 开发一个简单的 Spring Boot 应用程序。我的一项服务工作得很好。但是,当我定义另一个服务时,我得到了这个错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryService' defined in file [C:\Users\userName\Documents\Projects\ProjectA\backend\proj\build\classes\kotlin\main\c
om\example\test\category\service\CategoryService.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [c
om.example.test.category.service.CategoryService]: Constructor threw exception; nested exception is java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.
at org.springframework.beans.factory
当我删除此服务时,一切正常。所以添加第二个服务会导致这个问题。
//Service
@Service
class CategoryService : CategoryServiceInterface {
override fun deleteCategory(userToken: String, categoryId: String): String {
return ""
}
}
//Controller
@RestController
class CategoryController {
@Autowired
lateinit var categoryService: CategoryService
@DeleteMapping("/category")
fun deleteCategory(@RequestHeader(name = "Authorization") token: String, @RequestBody categoryId: String) =
categoryService.deleteCategory(
userToken = token,
categoryId = categoryId
)
//MainApp
@SpringBootApplication
class MainApplication
fun main(args: Array<String>) {
SpringApplication.run(MainApplication::class.java, *args)
}
FirebaseInitializer
@Service
class FirebaseInitializer {
@PostConstruct
fun initialize() {
try {
val serviceAccount = ClassPathResource("serviceaccount.json")
val options = FirebaseOptions
.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount.inputStream))
.setDatabaseUrl("https://databaseurl.firebaseio.com")
.build()
FirebaseApp.initializeApp(options)
} catch (e: Exception) {
}
}}
我用这篇文章设置了我的第一个服务https://medium.com/techwasti/spring-boot-firebase-crud-b0afab27b26e
如果我删除第二项服务,我的第一项服务将完全正常。任何帮助将不胜感激
【问题讨论】:
标签: firebase spring-boot kotlin