Skip to content

Consider merging spring.autoconfigure.exclude from multiple profiles #27414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
quaff opened this issue Jul 20, 2021 · 5 comments
Closed

Consider merging spring.autoconfigure.exclude from multiple profiles #27414

quaff opened this issue Jul 20, 2021 · 5 comments
Labels
status: duplicate A duplicate of another issue

Comments

@quaff
Copy link
Contributor

quaff commented Jul 20, 2021

---
spring:
  config.activate.on-profile: nosecurity
  autoconfigure:
    exclude: 
      - org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
      - org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration
 
---
spring:
  config.activate.on-profile: noredis
  autoconfigure:
    exclude: 
      - org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

if noredis,nosecurity active

expected:

  1. org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
  2. org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration
  3. org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

actual:

  1. org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
  2. org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 20, 2021
@wilkinsona
Copy link
Member

Thanks for the suggestion but this is working as designed and documented. Please see #12444 (comment) for links to a number of issues with further discussion on the matter.

@wilkinsona wilkinsona added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 20, 2021
@quaff
Copy link
Contributor Author

quaff commented Jul 21, 2021

@wilkinsona I know it's designed, my request is treat spring.autoconfigure.exclude specially and document it, such use case is very common, please reconsider. or guide me how to separate autoconfigure exclusions to multiple profiles.

@wilkinsona
Copy link
Member

That wasn’t clear to me from your original description which showed expected behaviour that is contrary to the documentation. That read like a bug report to me. A little bit of time explaining what you already now and what you are trying to do (rather than how you are trying to do it) would have been very helpful.

We can’t make an exception for this one property. It would be confusing for it to behave differently and would break for anyone relying on the current behaviour.

You haven’t really described what you are trying to do so offering advice is tricky. You could perhaps use the classpath to control which auto-configuration is active. Alternatively, as described in the issues linked to above, you can use some repetition in your configuration files. You would combine this with a profile expression such as noredis & nosecurity. If this becomes unwieldy you could also consider some logic in your main method or an environment post-processor the determines everything to exclude and sets the property.

If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

@gavenkoa
Copy link

I faced with the same issue when attempted to modularize config: during local development I want selectively disable features via profiles, like no-redis, no-email, no-gcp-pubsub, etc.

Naive attempt was:

---
spring.config.activate.on-profile: no-redis-cache
spring.autoconfigure.exclude:
  - org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
  - org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
---
spring.config.activate.on-profile: no-gcp-pubsub
spring.autoconfigure.exclude:
  - org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration
  - org.springframework.cloud.gcp.autoconfigure.pubsub.GcpPubSubAutoConfiguration

If two profiles are active only one exclude takes place, see #12444

To archive the goal of joint exclusions I used the feature introduced in #12586, see https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes#auto-configuration-exclusion

public class SaPubSubConfiguration {
    @Configuration
    @Profile("no-gcp-pubsub")
    @ImportAutoConfiguration(
            exclude = {
                    org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration.class,
                    org.springframework.cloud.gcp.autoconfigure.pubsub.GcpPubSubAutoConfiguration.class,
            })
    public static class DisabledConfig { }

    @Configuration
    @Profile("!no-gcp-pubsub")
    public static class EnabledConfig {}
}
public class RedisCacheManagerConfiguration {
    @Configuration
    @Profile("no-redis-cache")
    @ImportAutoConfiguration(
            exclude = {
                    org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration.class,
                    org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration.class,
            })
    public static class DisabledConfig { }

    @Configuration
    @Profile("!no-redis-cache")
    @EnableCaching
    public static class EnabledConfig { }
}

@gavenkoa
Copy link

When debugging of exclude set breakpoint in AutoConfigurationImportSelector.selectImports():

Set<String> allExclusions = this.autoConfigurationEntries.stream()
        .map(AutoConfigurationEntry::getExclusions).flatMap(Collection::stream).collect(Collectors.toSet());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants