I was recently migrating an application from Spring Boot 1.5.X to Spring Boot 2.X and saw an issue with overriding Spring Bean definitions. One of the configurations was along these lines in Kotlin:
Now, for a test I wanted to override these 2 bean definitions and did something along these lines:
This type of overriding works with Spring Boot 1.5.X but fails with Spring Boot 2.1.X with an error:
I feel this behavior is right, not allowing beans to overridden this way is the correct default behavior for an application, however I do want the ability to override the beans for tests and thanks to a Stack Overflow answer and Spring Boot 2.1.X release notes, the fix is to allow overrides using a property "spring.main.allow-bean-definition-overriding=true", so with this change, the test looks like this:
@Configuration
class DynamoConfig {
@Bean
fun dynamoDbAsyncClient(dynamoProperties: DynamoProperties): DynamoDbAsyncClient {
...
}
@Bean
fun dynampoDbSyncClient(dynamoProperties: DynamoProperties): DynamoDbClient {
...
}
}
Now, for a test I wanted to override these 2 bean definitions and did something along these lines:
@SpringBootTest
class DynamoConfigTest {
@Test
fun saveHotel() {
val hotelRepo = DynamoHotelRepo(localDynamoExtension.asyncClient!!)
val hotel = Hotel(id = "1", name = "test hotel", address = "test address", state = "OR", zip = "zip")
val resp = hotelRepo.saveHotel(hotel)
StepVerifier.create(resp)
.expectNext(hotel)
.expectComplete()
.verify()
}
@TestConfiguration
class SpringConfig {
@Bean
fun dynamoDbAsyncClient(dynamoProperties: DynamoProperties): DynamoDbAsyncClient {
...
}
@Bean
fun dynamoDbSyncClient(dynamoProperties: DynamoProperties): DynamoDbClient {
...
}
}
}
This type of overriding works with Spring Boot 1.5.X but fails with Spring Boot 2.1.X with an error:
Invalid bean definition with name 'dynamoDbAsyncClient' defined in sample.dyn.repo.DynamoConfigTest$SpringConfig:..
There is already .. defined in class path resource [sample/dyn/config/DynamoConfig.class]] bound
I feel this behavior is right, not allowing beans to overridden this way is the correct default behavior for an application, however I do want the ability to override the beans for tests and thanks to a Stack Overflow answer and Spring Boot 2.1.X release notes, the fix is to allow overrides using a property "spring.main.allow-bean-definition-overriding=true", so with this change, the test looks like this:
@SpringBootTest(properties = ["spring.main.allow-bean-definition-overriding=true"])
class DynamoConfigTest {
@Test
fun saveHotel() {
val hotelRepo = DynamoHotelRepo(localDynamoExtension.asyncClient!!)
val hotel = Hotel(id = "1", name = "test hotel", address = "test address", state = "OR", zip = "zip")
val resp = hotelRepo.saveHotel(hotel)
StepVerifier.create(resp)
.expectNext(hotel)
.expectComplete()
.verify()
}
@TestConfiguration
class SpringConfig {
@Bean
fun dynamoDbAsyncClient(dynamoProperties: DynamoProperties): DynamoDbAsyncClient {
...
}
@Bean
fun dynamoDbSyncClient(dynamoProperties: DynamoProperties): DynamoDbClient {
...
}
}
}