BeanInstantitationException: Failed to Instantiate Service

Viewed 210

I am developing a simple spring boot application in Kotlin. I have one of my service working completely fine. However when i define another service i get this error

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

When i remove this service, everything works fine. So adding second service causes this issue.

//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) {

    }
}}

I used this article to set up my first service https://medium.com/techwasti/spring-boot-firebase-crud-b0afab27b26e

If i remove this second service, my first service works completely fine. Any help would be greatly appreciated

1 Answers

So i found the problem, If i initialize the firebase with the way mentioned in the article above as a service and using post construct, it gives me the above issues.

I moved my firebase initialize code in my Spring boot application class and everything now works as expected.

@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)
}
Related