2017-08-16 424 views
0

我们正在定义我们公司的微服务基础架构。我们创建了我们自己的“父母”,它为我们的服务定义了物料清单。此外,我们还有几个可用于我们服务的“启动器”项目和小型图书馆。 (例如,服务可以包含“starter-stream”以包含Kafka的依赖关系)。Spring Boot - 以编程方式禁用AutoConfiguration

流式应用程序库为Kafka设置提供了自己的自动配置,并要求禁用Kafka的默认AutoConfiguration。这很简单,我们可以要求任何使用该库的微服务添加“排除”。

我在寻找的是一种以编程方式执行此操作的方式,因此我们不必在每个Web服务中添加排除项。

我知道这可能是一种独特的情况,我想这样做的一种可能方式是将EnvironmentPostProcessor添加到我们的实用程序库中,该实用程序库将排除项添加到spring.autoconfigure.exclude。如果属性已经存在,我们可以使其足够聪明以连接排除。

有没有一种更优雅的方式来做这种事情?

回答

1

我认为你的建议可以通过EnvironmentPostProcessor来修改spring.auconfigure.exclude

另一种时髦的方式可能是将org.springframework.boot.autoconfigure.AutoConfigurationImportSelector继承并覆盖org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getExclusions,以便它将已配置的排除与已添加的排除组合在一起。

public class MyCustomSelector extends AutoConfigurationImportSelector { 
    @Override 
    protected Set<String> getExclusions(AnnotationMetadata metadata, AnnotationAttributes attributes) { 
    Set<String> exclusions = super.getExclusions(metadata, attributes); 
    exclusions.add("some.other.config.Configuration"); 
    return exclusions; 
    } 
} 

然后你就可以使用它与@Import(MyCustomSelector.class)

+0

要跟进这个问题,这是否意味着如果我想重写默认选择器,那我必须有@SpringBootApplication的自定义变体来导入我的选择器而不是默认选择器? – Tyler

相关问题