Background
In our use case, we need to support connecting to multiple Kafka servers. Spring does not support this. We want to use Spring's KafkaTemplate, but when spring-kafka and spring boot are both on the classpath, KafkaAutoConfiguration is automatically activated, and there is no configuration like spring.kafka.enabled to disable the auto-configuration.
Why not use @SpringBootApplication(exclude = KafkaAutoConfiguration.class) to exclude the auto-configuration class?
Changing code for every service just sucks... We will provide a library to support configuring multiple Kafka servers, like creating our own Spring Boot starter.
More thoughts
Consider providing a spring.<module>.enabled configuration for Spring Boot modules (e.g., spring.datasource.enabled, spring.redis.enabled). When users develop based on Spring Boot, if there is this configuration, they do not need to force auto-configuration because sometimes users just want to use Spring's code (JdbcTemplate, RedisTemplate), not auto-configuration. I have written some Spring Boot starters, and I always provide an enabled configuration for a feature (with a reasonable default value). It works very well and makes testing easier.
Comment From: mhalbritter
Btw, you can also exclude auto-configurations via properties. That way you can set it via the environment and don't have to change code.
Does that solve your problem?
Comment From: quaff
Btw, you can also exclude auto-configurations via properties. That way you can set it via the environment and don't have to change code.
Does that solve your problem?
See https://github.com/spring-projects/spring-boot/issues/38641.
Comment From: quaff
I'd prefer https://github.com/spring-projects/spring-boot/issues/41669, is it suitable for your use case? @DanielLiu1123
Comment From: DanielLiu1123
Btw, you can also exclude auto-configurations via properties.
I did miss that configuration, but I think manually excluding auto-configuration is not suitable.
To support connecting to multiple Kafka servers, I need to disable Kafka auto-configuration in the library. However, some services use KafkaTemplate provided by Kafka auto-configuration, so Kafka auto-configuration needs to be enabled in some services. Manually excluding auto-configuration in the library and then enabling it in the app configuration is very strange. But if there is an enabled configuration, I only need to set spring.kafka.enabled=false in the library, and for those services that rely on auto-configuration, just set spring.kafka.enabled=true.
That way you can set it via the environment and don't have to change code.
In our team, we avoid using environment variables or system properties to override configurations. We only inject sensitive data using environment variables. We want all configurations to be explicitly declared.
Comment From: DanielLiu1123
I'd prefer https://github.com/spring-projects/spring-boot/issues/41669, is it suitable for your use case?
I still don’t think manually excluding auto-configuration is a good way. For users, providing an enabled configuration for a feature/functionality is simpler and clearer.
Comment From: quaff
I'd prefer #41669, is it suitable for your use case?
I still don’t think manually excluding auto-configuration is a good way. For users, providing an
enabledconfiguration for a feature/functionality is simpler and clearer.
spring.kafka.enabled is opposite to spring.autoconfigure.exclusion.org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,
the advantage of spring.*.enabled is more shorter and readable, but the disadvantage is obvious, Spring Boot need add the configuration key for every AutoConfiguration which is cumbersome and error-prone.
I'd prefer spring.autoconfigure.exclusion.* and alias spring.autoconfigure.exclusion.org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration=${spring.kafka.disabled:false}.
Comment From: mhalbritter
I'm flagging this so that we can discuss it in our next team meeting. The enabled properties for auto-config come up here and there.