I'm writing configuration properties and would like to use data class to hold the data.
Problem is, data classes have a primary constructor and are immutable, and micronaut tries to inject the values as beans.
Example:
@ConfigurationProperties("gerencianet")
data class GerenciaNetConfiguration(
val clientId: String,
val clientSecret: String,
val apiUrl: String,
val notificationUrl: String,
val datePattern: String = "yyyy-MM-dd HH:mm:ss"
)
Error: Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [java.lang.String] exists. Make sure the bean is not disabled by bean requirements
Is there support for it?
markfickett :
You can inject the values as constructor parameters using @Parameter. It avoids common mistakes with @Value.\nFor example, if your application.yml looks like this:\ngroup:\n first-value: asdf\n second-value: ghjk\n\nThen a Kotlin class might look like:\nimport io.micronaut.context.annotation.Property\nimport javax.inject.Singleton\n\n@Singleton\nclass MyClass(@Property(name = "group.first-value") val firstValue: String) {\n fun doIt(): String {\n return firstValue\n }\n}\n\nOr, similarly, a method:\nimport io.micronaut.context.annotation.Factory\nimport io.micronaut.context.annotation.Property\nimport javax.inject.Singleton\n\n@Factory\nclass MyFactory {\n @Singleton\n fun getSomeValue(@Property(name = "group.first-value") firstValue: String): SomeClass {\n return SomeClass.newBuilder()\n .setTheValue(firstValue)\n .build()\n }\n}\n",
2020-11-03T01:28:21