Spring Boot:用另一个类替换@Configuration类



我有一个自定义配置类,我在引导期间使用弹簧工厂加载。问题是它被来自spring ** starter包的另一个类似的配置类覆盖。我试过排除第二个,但它仍然加载。我也试过设置优先级,但也没用。

下面是我的自定义配置类的一个片段:
@Slf4j
@Configuration
@RequiredArgsConstructor
public class CustomAwsParamStorePropertySourceLocatorConfig implements PropertySourceLocator
...

我想要排除的是来自spring boot aws starter:

public class AwsParamStorePropertySourceLocator implements PropertySourceLocator {

AwsParamStoreBootstrapConfiguration类在类级别有ConditionalOnProperty注释…

@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);
}

所以如果你配置了属性aws.paramstore.enabled=false,它应该阻止该配置创建AwsParamStorePropertySourceLocator bean。

需要注意的是,这也会停止在AwsParamStoreBootstrapConfiguration类中创建的AWSSimpleSystemsManagement bean的创建,所以如果您需要该bean,您可能还需要在您的自定义配置类中创建它。

@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();
}

最新更新