日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring-Boot 2.1.x和主要的bean定义

發布時間:2023/12/3 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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定义的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。