Skip to content
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

ConditionalOn annotation equivalents for EnvironmentPostProcessors #44933

Closed
vab2048 opened this issue Mar 28, 2025 · 1 comment
Closed

ConditionalOn annotation equivalents for EnvironmentPostProcessors #44933

vab2048 opened this issue Mar 28, 2025 · 1 comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@vab2048
Copy link

vab2048 commented Mar 28, 2025

Enhancement

I am trying to have my EnvironmentPostProcessor run conditionally like my autoconfiguration.

For autoconfigurations there are useful annotations @ConditionalOnClass and @ConditionalOnProperty.

Are there equivalent annotations for this which can be used by an EnvironmentPostProcessor?
I cannot find any and so my feature request is to add them.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 28, 2025
@wilkinsona
Copy link
Member

There's no annotation support for EnvironmentPostProcessor implementations due to how early they are created which is before the application context exists. Instead of conditions or equivalent annotations, you should use logic in your implementation.

You can use org.springframework.util.ClassUtils.isPresent(String, ClassLoader) as an alternative to @ConditionalOnClass.

A robust equivalent of @ConditionalOnProperty isn't possible in an EnvironmentPostProcessor. Check for a property requires the Environment to have been created and prepared. Part of that preparation is running all of the registered EnvironmentPostProcessor implementations. If a post-processor runs after yours and adds a property or changes a property's value, your post-processor may have done the wrong thing. If you're happy with this ordering limitation, you could use org.springframework.core.env.PropertyResolver.getProperty(String) to check for a property's existence and examine its value.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2025
@wilkinsona wilkinsona added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 28, 2025
@philwebb philwebb changed the title ConditionalOn annotation equivalents for EnvironmentPostProcessors ConditionalOn annotation equivalents for EnvironmentPostProcessors Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

3 participants