Недавно я переносил приложение из Spring Boot 1.5.X в Spring Boot 2.X и увидел проблему с переопределением определений Spring Bean. Одна из конфигураций была в этих направлениях в Котлине:
1
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @Configuration class DynamoConfig { @Bean fun dynamoDbAsyncClient(dynamoProperties: DynamoProperties): DynamoDbAsyncClient { ... } @Bean fun dynampoDbSyncClient(dynamoProperties: DynamoProperties): DynamoDbClient { ... } }</span> @Configuration class DynamoConfig { @Bean fun DynamoDbAsyncClient (DynamoProperties: DynamoProperties): DynamoDbAsyncClient {...} @Bean fun dynampoDbSyncClient (DynamoProperties: DynamoProperties): DynamoDbClient {...}}</span> |
Теперь для теста я хотел переопределить эти 2 определения бинов и сделал что-то вроде этого:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @SpringBootTest </span> @SpringBootTest </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > class DynamoConfigTest {</span> class DynamoConfigTest {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @Test </span> @Тест</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >fun saveHotel() {</span> весело saveHotel () {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >val hotelRepo = DynamoHotelRepo(localDynamoExtension.asyncClient!!)</span> val hotelRepo = DynamoHotelRepo (localDynamoExtension.asyncClient !!)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >val hotel = Hotel(id = "1" , name = "test hotel" , address = "test address" , state = "OR" , zip = "zip" )</span> val hotel = Hotel (id = "1" , name = "тестовый отель" , address = "тестовый адрес" , state = "OR" , zip = "zip" )</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >val resp = hotelRepo.saveHotel(hotel)</span> val resp = hotelRepo.saveHotel (отель)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >StepVerifier.create(resp)</span> StepVerifier.create (соответственно)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >.expectNext(hotel)</span> .expectNext (отель)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >.expectComplete()</span> .expectComplete ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >.verify()</span> .verify ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @TestConfiguration </span> @TestConfiguration </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > class SpringConfig {</span> class SpringConfig {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @Bean </span> @Bean </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >fun dynamoDbAsyncClient(dynamoProperties: DynamoProperties): DynamoDbAsyncClient {</span> забавный DynamoDbAsyncClient (DynamoProperties: DynamoProperties): DynamoDbAsyncClient {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >...</span> ...</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @Bean </span> @Bean </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >fun dynamoDbSyncClient(dynamoProperties: DynamoProperties): DynamoDbClient {</span> забавный DynamoDbSyncClient (DynamoProperties: DynamoProperties): DynamoDbClient {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >...</span> ...</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> |
Этот тип переопределения работает с Spring Boot 1.5.X, но завершается с Spring Boot 2.1.X с ошибкой:
1
2
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >Invalid bean definition with name 'dynamoDbAsyncClient' defined in sample.dyn.repo.DynamoConfigTest$SpringConfig:..</span> Неверное определение bean-компонента с именем «dynamicoDbAsyncClient», определенное в sample.dyn.repo.DynamoConfigTest $ SpringConfig: ..</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >There is already .. defined in class path resource [sample/dyn/config/DynamoConfig. class ]] bound</span> Там уже .. определено в пути к ресурсам класса [sample / dyn / config / DynamoConfig. class ]] привязано</span> |
Я чувствую, что это правильное поведение, не позволяя переопределять bean-компоненты таким образом, это правильное поведение по умолчанию для приложения, однако я хочу иметь возможность переопределять bean-компоненты для тестов и благодаря ответу Stack Overflow и примечаниям к выпуску Spring Boot 2.1.X исправление состоит в том, чтобы разрешить переопределения, используя свойство «spring.main.allow-bean-definition-overriding = true», поэтому с этим изменением тест выглядит следующим образом:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @SpringBootTest (properties = [ "spring.main.allow-bean-definition-overriding=true" ])</span> @SpringBootTest (properties = [ "spring.main.allow-bean-definition-overriding = true" ])</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > class DynamoConfigTest {</span> class DynamoConfigTest {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @Test </span> @Тест</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >fun saveHotel() {</span> весело saveHotel () {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >val hotelRepo = DynamoHotelRepo(localDynamoExtension.asyncClient!!)</span> val hotelRepo = DynamoHotelRepo (localDynamoExtension.asyncClient !!)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >val hotel = Hotel(id = "1" , name = "test hotel" , address = "test address" , state = "OR" , zip = "zip" )</span> val hotel = Hotel (id = "1" , name = "тестовый отель" , address = "тестовый адрес" , state = "OR" , zip = "zip" )</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >val resp = hotelRepo.saveHotel(hotel)</span> val resp = hotelRepo.saveHotel (отель)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >StepVerifier.create(resp)</span> StepVerifier.create (соответственно)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >.expectNext(hotel)</span> .expectNext (отель)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >.expectComplete()</span> .expectComplete ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >.verify()</span> .verify ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @TestConfiguration </span> @TestConfiguration </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > class SpringConfig {</span> class SpringConfig {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @Bean </span> @Bean </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >fun dynamoDbAsyncClient(dynamoProperties: DynamoProperties): DynamoDbAsyncClient {</span> забавный DynamoDbAsyncClient (DynamoProperties: DynamoProperties): DynamoDbAsyncClient {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >...</span> ...</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @Bean </span> @Bean </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >fun dynamoDbSyncClient(dynamoProperties: DynamoProperties): DynamoDbClient {</span> забавный DynamoDbSyncClient (DynamoProperties: DynamoProperties): DynamoDbClient {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >...</span> ...</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> |
Смотрите оригинальную статью здесь: Spring-Boot 2.1.x и определение переопределенного бина Мнения, высказанные участниками Java Code Geeks, являются их собственными. |