【问题标题】:BeanInstantitationException: Failed to Instantiate ServiceBeanInstantiationException:无法实例化服务
【发布时间】: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


    【解决方案1】:

    所以我发现了问题,如果我用上面文章中提到的方式将firebase初始化为服务并使用post构造,它会给我上面的问题。

    我将我的 Firebase 初始化代码移到了我的 Spring Boot 应用程序类中,现在一切都按预期工作了。

    @SpringBootApplication
    class FoodiesApplication
    
    fun main(args: Array<String>) {
    
    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) {
    
    }
    SpringApplication.run(FoodiesApplication::class.java, *args)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 2014-07-27
      • 2011-06-28
      • 2012-08-04
      • 1970-01-01
      • 2023-03-06
      相关资源
      最近更新 更多