79536131

Date: 2025-03-26 10:34:39
Score: 1
Natty:
Report link

You can also use @PostConstruct as well...

@Service
class ServerConfigurationService(
    private val serverConfigurationRepository: ServerConfigurationRepository
) {

    private val logger = LoggerFactory.getLogger(ServerConfigurationService::class.java)

    fun createServerConfiguration() {
        if (serverConfigurationRepository.count() == 0L) {
            try {
                serverConfigurationRepository.save(ServerConfiguration())
            } catch (e: Exception) {
                logger.error("Error while creating server configuration: ${e.message}", e)
            }
        } else {
            logger.info("Server configuration already present")
        }
    }

    @PostConstruct
    fun init() {
        logger.info("Initializing server configuration...")
        createServerConfiguration()
    }
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @PostConstruct
  • Low reputation (1):
Posted by: user3292013