Hey guys, I have a simple data class for configura...
# spring
a
Hey guys, I have a simple data class for configuration like the following
Copy code
@Configuration
@ConfigurationProperties(prefix = "my-config")
data class MyConfig(
    var blacklist: List<BlackListEntry> = emptyList(),
)

data class BlackListEntry(
    val name: String,
    val values: List<String> = emptyList(),
)
And I'm using
spring.config.import
to import an additional application.yaml in my environment. The thing is I get
java.lang.IllegalStateException: No setter found for property: blacklist
When I change the blacklist to be a MutableList it works, does spring first instantiate MyConfig for the base application.yaml and then try to call the setters with the additional imports? Shouldn't it merge the yamls first and only call the constructor once? Any ideias how can I keep the list as immutable? Im using spring-boot 3.2 btw Thanks in advance ✌️
d
I'm using a similar pattern in my app. Only I'm using
val
everywhere. Did you try that?
a
ya im using val but the thing is its a List instead of a MutableList
d
I've found the exact same pattern, i.e.
val list: List<...> = ...
in my Spring app with imported config and it works for me. So perhaps the problem might be somewhere else?
a
Ya but are u importing other application yaml/properties?
using
spring.config.import
?
d
Yes, I'm using
spring.config.import
Actually, I'm importing ALL app properties this way, i.e. no mixing.
a
hmm
can u share the example you have?
might be some little detail
d
Sorry, it's a private project 😞 But I see that you are actually using var:
var blacklist =
a
i will try to setup a minimal setup that reproduces my issue
I believe the issue is with the "default"
when i remove
= emptyList()
it works
👍 1
p
I think you can go with a val blacklist, but with a
Copy code
mutableListOf()
default
j
Why would you make configuration properties mutable? They should come from the config and mutating them sounds like asking for trouble
p
True. But then why does it need setter?