Hello everyone! Does one of you got a hint for me ...
# spring
p
Hello everyone! Does one of you got a hint for me on how to properly inject configurationProperties in a
@Configuration
class? Usually, one would define it as follows:
Copy code
@ConstructorBinding
@ConfigurationProperties(prefix = "rooms")
class RoomConfigurationProperties(val filePath: String)
But due to modularization, I have a configuration class for the whole module (speak: maven module) whichs looks similiar to that:
Copy code
@Configuration
@ComponentScan
class RoomConfig {

    @Bean
    @ConfigurationProperties(prefix = "rooms")
    fun properties(filePath: String) = RoomConfigurationProperties(filePath)

    /*...*/
}
However, since the filepath is a constructor parameter, I of course have to supply it and I'm not sure how. Any hint would be appreciated, I unfortunately didn't find what I was looking for in the docs.
k
what are you trying to accomplish?
having access to properties inside RoomConfig to be used in other methods or just have your main module find submodule configProperties?
p
Excuse me if I go the long way now, but I think it could be worth it due to me missing something: I have a project with multiple different maven modules. Let's say
module1
,
module2
and
configuration
, where the latter holds the applications main class. After reading about (https://reflectoring.io/testing-verticals-and-layers-spring-boot/) how to properly test the modules without having a dependency on configuration (since config has a dependency on the other modules -> would be circular) I decided to have a configuration class per module which holds the info, so I can pass that class later on in my apps main class and to also use that class in my tests:
Copy code
@SpringBootTest(classes = [Module1Config::class], webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Since
Module1Config
holds all info, it also needs the `Module1ConfigurationProperties`` Bean to pass it into the other services. Problem there is that I have to provide the constructor arguments for my ConfigurationProperties (see message 1) and I fail to achieve that. That turned out to be longer than expected. Hit me up if it is too blurry.
I think one could rephrase the whole question: How to e2e test a module without having access to the main SpringBootApplication class. Diving into that brought me to my current problem.
s
Perhaps something like the following might help or give you some ideas if you’re using JUnit 5?
Copy code
@ExtendWith(SpringExtension::class)
@ContextConfiguration(initializers = [ConfigDataApplicationContextInitializer::class])
class MyTest { ... }
The
SpringExtension
will integrate a Spring TestContext and the
ContextConfiguration
will let you load config properties. Then you could just perhaps
@Import
your
RoomConfig
or whatever else you need?
k
as far as I understand spring will component scan for @ConfigurationProperties
so in your room config you should be able to do
Copy code
@Bean
fun someBean(roomConfigurationProperties:RoomConfigurationProperties): SomeBean {...}
p
That sounds good, will try that. I actually found another "workaround": I created a TestApp in my test directory which gets picked up when I run a test with
@SpringBootTest
.
k
hm but that's second part of the problem the article is solving, right? how to initialize just submodule
p
Exactly. I'm rewriting it right now to match that, let's see what I can achieve.
I got it working! So, to sum it up: I didn't think of spring picking up the ConfigurationProperties automatically. Thanks a lot, Jakub!
k
👍