I got a wired issue, and don't find any tips on it.
I'm working on a database sharding configuration with properties files. I got a class which has the responsability to load those properties:
@Component
@PropertySources(value = *arrayOf(
PropertySource("classpath:/dbconfig/base.properties"),
PropertySource("classpath:/dbconfig/override.properties", ignoreResourceNotFound = true)
))
@ConfigurationProperties("groups.datasource")
class DbConfig {
val base: List<DataSourceProperties> = ArrayList()
val override: List<DataSourceProperties> = ArrayList()
}
Then, I use this component in a configuration class to setup my datasrouces.
Here is my src/main/resources/dbconfig/base.properties file:
#Spring Boot Config for Oracle
# CENTRAL
groups.datasource.base[0].name=central
groups.datasource.base[0].url=jdbc:oracle:thin:@url:1530:sid
groups.datasource.base[0].username=user
groups.datasource.base[0].password=passwd
groups.datasource.base[0].driver=class name=oracle.jdbc.driver.OracleDriver
And my src/test/resources/dbconfig/override.properties file:
#Spring Boot Config for Oracle
# PREPROD
groups.base.override[1].name=preprod
groups.base.override[1].url=jdbc:oracle:thin:@user:1542:sid
groups.base.override[1].username=user
groups.base.override[1].password=passwd
groups.base.override[1].driver=class name=oracle.jdbc.driver.OracleDriver
And here is my test class:
@RunWith(SpringJUnit4ClassRunner::class)
@SpringBootTest(classes = arrayOf(DatabaseComponent::class))
open class DatabaseComponentTest {
@Autowired
lateinit var env: Environment
@Test
fun testBaseConfiguration() {
assert(env.getProperty("groups.datasource.base[0].name") == "central")
assert(env.getProperty("groups.datasource.base[0].url") == "jdbc:oracle:thin:@url:1530:sid")
assert(env.getProperty("groups.datasource.base[0].username") == "user")
assert(env.getProperty("groups.datasource.base[0].password") == "passwd")
assert(env.getProperty("groups.datasource.base[0].driver") == "class name=oracle.jdbc.driver.OracleDriver")
}
@Test
fun testOverrideConfiguration() {
assert(env.getProperty("groups.datasource.override[0].name") == "preprod")
assert(env.getProperty("groups.datasource.override[0].url") == "jdbc:oracle:thin:@url:1542:sid")
assert(env.getProperty("groups.datasource.override[0].username") == "user")
assert(env.getProperty("groups.datasource.override[0].password") == "passwd")
assert(env.getProperty("groups.datasource.override[0].driver") == "class name=oracle.jdbc.driver.OracleDriver")
}
}
In debug mode, when I look into the classLoader, the file override.properties seems to be loaded with all his values.
Plus, testBaseConfiguration does work, and assert true. While, testOverrideConfiguration doesn't, and assert false.
Another thing, all that behavior works well in a Spring module, override are here and loaded. My issue is just for my test case.
Any idea of what am I doing wrong ?
EDIT 1: Here is DataBaseComponent:
@Configuration
@EnableAutoConfiguration
@ComponentScan
open class DatabaseComponent
I also put my code on GitHub : https://github.com/romainbsl/spring-database-sharding