當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring-Boot 2.1.x和主要的bean定义
生活随笔
收集整理的這篇文章主要介紹了
Spring-Boot 2.1.x和主要的bean定义
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我最近將應用程序從Spring Boot 1.5.X遷移到Spring Boot 2.X ,發現覆蓋Spring Bean定義存在問題。 其中一種配置是在Kotlin中遵循的:
@Configuration class DynamoConfig {@Beanfun dynamoDbAsyncClient(dynamoProperties: DynamoProperties): DynamoDbAsyncClient {...}@Beanfun dynampoDbSyncClient(dynamoProperties: DynamoProperties): DynamoDbClient {...} }現在,為了進行測試,我想覆蓋這兩個bean定義,并按照以下原則進行操作:
@SpringBootTest class DynamoConfigTest {@Testfun 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()}@TestConfigurationclass SpringConfig {@Beanfun dynamoDbAsyncClient(dynamoProperties: DynamoProperties): DynamoDbAsyncClient {...}@Beanfun dynamoDbSyncClient(dynamoProperties: DynamoProperties): DynamoDbClient {...}} }這種覆蓋類型適用于Spring Boot 1.5.X,但不適用于Spring Boot 2.1.X,并顯示以下錯誤:
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我認為這種行為是正確的,不允許以這種方式覆蓋bean是應用程序的正確默認行為,但是我確實希望能夠覆蓋bean進行測試,這要歸功于Stack Overflow答案和Spring Boot 2.1.X發行說明 ,解決方法是允許使用屬性“ spring.main.allow-bean-definition-overriding = true”進行覆蓋,因此,通過進行此更改,測試如下所示:
@SpringBootTest(properties = ["spring.main.allow-bean-definition-overriding=true"]) class DynamoConfigTest {@Testfun 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()}@TestConfigurationclass SpringConfig {@Beanfun dynamoDbAsyncClient(dynamoProperties: DynamoProperties): DynamoDbAsyncClient {...}@Beanfun dynamoDbSyncClient(dynamoProperties: DynamoProperties): DynamoDbClient {...}} }翻譯自: https://www.javacodegeeks.com/2019/01/spring-boot-overriding-bean-definition.html
總結
以上是生活随笔為你收集整理的Spring-Boot 2.1.x和主要的bean定义的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qq安全中心手机版(安全中心手机版官网)
- 下一篇: 电脑快捷键切换网络(电脑快捷键切换网络设