Deniss M.
Deniss M.

Reputation: 4050

Spring Boot: substitute @Configuration class with another

I have a custom configuration class that I am loading using spring factories during bootstrap. The problem is that it is being overwritten by another similar configuration class coming from a spring ** starter package. I've tried excluding the second one, but it still loads. Also tried to set priorities, but that didn't work too.

Here's a snippet of my custom configuration class:

@Slf4j
@Configuration
@RequiredArgsConstructor
public class CustomAwsParamStorePropertySourceLocatorConfig implements PropertySourceLocator
...

And the one I'm trying to exclude that is coming from spring boot aws starter:

public class AwsParamStorePropertySourceLocator implements PropertySourceLocator {

Upvotes: 2

Views: 729

Answers (1)

httPants
httPants

Reputation: 2123

The AwsParamStoreBootstrapConfiguration class has the ConditionalOnProperty annotation at the class level...

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(AwsParamStoreProperties.class)
@ConditionalOnClass({ AWSSimpleSystemsManagement.class, AwsParamStorePropertySourceLocator.class })
@ConditionalOnProperty(prefix = AwsParamStoreProperties.CONFIG_PREFIX, name = "enabled", matchIfMissing = true)
public class AwsParamStoreBootstrapConfiguration {

    private final Environment environment;

    public AwsParamStoreBootstrapConfiguration(Environment environment) {
        this.environment = environment;
    }

    @Bean
    AwsParamStorePropertySourceLocator awsParamStorePropertySourceLocator(AWSSimpleSystemsManagement ssmClient,
            AwsParamStoreProperties properties) {
        if (StringUtils.isNullOrEmpty(properties.getName())) {
            properties.setName(this.environment.getProperty("spring.application.name"));
        }
        return new AwsParamStorePropertySourceLocator(ssmClient, properties);
    }

So if you configured the property aws.paramstore.enabled=false it should stop that configuration from creating the AwsParamStorePropertySourceLocator bean.

It's important to note, that would also stop the creation of the AWSSimpleSystemsManagement bean which is also created in the AwsParamStoreBootstrapConfiguration class, so if you require that bean, you may need to also create it in your custom Configuration class.

@Bean
@ConditionalOnMissingBean
AWSSimpleSystemsManagement ssmClient(AwsParamStoreProperties properties) {
    return createSimpleSystemManagementClient(properties);
}

public static AWSSimpleSystemsManagement createSimpleSystemManagementClient(AwsParamStoreProperties properties) {
    AWSSimpleSystemsManagementClientBuilder builder = AWSSimpleSystemsManagementClientBuilder.standard()
            .withClientConfiguration(SpringCloudClientConfiguration.getClientConfiguration());
    if (!StringUtils.isNullOrEmpty(properties.getRegion())) {
        builder.withRegion(properties.getRegion());
    }
    if (properties.getEndpoint() != null) {
        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
                properties.getEndpoint().toString(), null);
        builder.withEndpointConfiguration(endpointConfiguration);
    }
    return builder.build();
}

Upvotes: 1

Related Questions