I have a wired problem and cannot find any advice in it.
I am working on setting up a database configuration with property files. I got a class that is responsible for loading these 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 the configuration class to configure my data.
Here is my src / main / resources / dbconfig / base.properties file:
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 the 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 at the Loader class, the override.properties file seems to be loaded with all its values.
Also, testBaseConfiguration really works, and state true. For now, testOverrideConfiguration does not, and state false.
, Spring, . .
, ?
1: DataBaseComponent:
@Configuration
@EnableAutoConfiguration
@ComponentScan
open class DatabaseComponent
GitHub: https://github.com/romainbsl/spring-database-sharding