EricJTurley
06/21/2021, 5:35 PM@ConfigurationProperties("chat-replay")
@ConstructorBinding
data class ChatReplayProperties(
val chatLogPrintInterval: Long,
val poll: Poll,
val generation: Generation,
val query: Query,
val external: External,
) {
But:
• All the chat-replay.generation.*
properties are in a application-genchatlogs.properties
file, so...
• When run with no active Spring Profiles, the Generation
object is null
And application startup fails with ParameterResolutionException
of course.
What's a good pattern for this?EricJTurley
06/21/2021, 5:38 PMChatReplayProperties.Generation
as nullable, I have to keep treating all accesses of the property specially.Saharath Kleips
06/21/2021, 6:04 PMlateinit
? But it feels like there might be something better…hho
06/22/2021, 8:42 AMhho
06/22/2021, 8:42 AMGeneration
isn't too complex, you could also put a default with dummy valuesthanksforallthefish
06/22/2021, 12:35 PM@ConfigurationProperties("chat-replay")
@ConstructorBinding
data class ChatReplayProperties(
val chatLogPrintInterval: Long,
val poll: Poll,
val generation: Generation?,
val query: Query,
val external: External,
) {
val publicGeneration: Generation
get() = checkNotNull(generation)
}
I don’t remember right now if you could have val generation: Generation?
private and still have it injected by springEricJTurley
06/23/2021, 6:27 PMlateinit
- but then I don't get constructor injection.EricJTurley
06/23/2021, 6:28 PMGeneration
-type properties don't apply UNLESS the gen-chat-logs
profile is active. So ... it doesn't strictly belong in application.properties
(which is why it's currently not there, and this problem exists)EricJTurley
06/23/2021, 6:29 PMapplication.properties
just to create an object that isn't used or read ... of course.
But I'm looking for a better pattern...EricJTurley
06/23/2021, 6:31 PMproperties.getGeneration().getSomething()
might throw an NPE. Not too big a deal.
But calling it from Kotlin requires treating it as strictly nullable - which I can do, but, practically, it's never null WHEN it's called, because it's only ever called from classes that are also active when that profile is active.
So - I'm using this, but wondering if there was some better pattern.thanksforallthefish
06/23/2021, 6:41 PMgeneration
and not a non null version? you are not forced to use the same value you injected, practically I never do so I can preserve encapsulation and I feel more OO. just use publicGeneration
which will ofc fails when generation
is null, but since it cannot happen it is not a concern