Skip to content

spring scanning same type bean with different names returning ambiguous exception after upgrading to spring-boot 3.2 #31743

Closed as not planned
@VelDeveloper

Description

@VelDeveloper

Hi,

I recently upgraded my application to spring-boot 3.2 from spring-boot 3.0.x. After upgrading we are getting several bean ambiguous exceptions. This was working fine before.

The Application is consuming several client libraries and each client library is creating RestTemplate using the below code.

@RequiredArgsConstructor
@AutoConfiguration
@ConfigurationPropertiesScan
@ComponentScan(basePackages = {"com.partner.tracker.client"})
public class TrackerClientConfiguration {

	@Autowired(required = false)
	private RestTemplateCustomizer restTemplateCustomizer;
	
	@Bean(name = "trackerRestTemplate")
	@ConditionalOnMissingBean(name = "trackerRestTemplate")
	public RestTemplate trackerRestTemplate() {
		return createTrackerRestTemplate();
	}

	public RestTemplate createTrackerRestTemplate() {
		RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder()
				.requestFactory(this::customHttpRequestFactory)
				.interceptors(
						getDefaultHeadersInterceptor(),
						getTraceIdInterceptor()
				)
				.messageConverters(getMessageConverters());
		if (Objects.nonNull(restTemplateCustomizer)) {
			restTemplateBuilder = restTemplateBuilder.additionalCustomizers(restTemplateCustomizer);
		}
		return restTemplateBuilder.build();
	}
}

We are using the same code for other 5-libraries RestTemplate creation.

This was working fine till we upgraded to spring-boot 3.2, but we got the below exception after upgrading to 3.2.

Parameter 1 of constructor in com.tracker.client.TrackerApiClient required a single bean, but 6 were found:

- ulfRestTemplate: defined in file <file location>
- trackerRestTemplate: defined in file <file location>
- homeRestTemplate: defined by method
- pluginRestTemplate: defined by method
- partnerRestTemplate: defined by method
- goRestTemplate: defined by method

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

So we are using @Qualifier annotation temporarily to fix this issue, but I don't want to do that since it breaks unit testing. Also I use Lombok for constructor injection. Now I should go with manual constructor with @Qualifier annotation.

So this bug was recently introduced by the spring team or is this how Spring always works for the same type of bean? If this is the usual behavior then I wonder how it works in the past.

Any help would be really appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions