Search code examples
springkotlinspring-cloud-config

AopConfigException: Could not generate CGLIB subclass of class error on creating the instance of my kotlin data class


I am getting the error on my container start :

org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class com.config.FeatureFlags: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class com.config.FeatureFlags

My Classes are as below

data class FeatureFlags(val name: String, val password: String)

And my Bean

@Configuration


class MyAppConfig {
@Value("\${myapp.username}")
var username: String

@Value("\${myapp.password}")
var password: String

@RefreshScope
@Bean
fun myBean(): FeatureFlags{
    return FeatureFlags(username, password)
} 
}

What is the error that I am doing here?


Solution

  • @RefreshScope needs to be able to create a subclass of your FeatureFlags class. But your class is data class - so it is effectively final and thus cannot be extended.

    From docs: Data classes cannot be abstract, open, sealed, or inner.

    You need to define open class FeatureFlags.