-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Add edge case docs of the value() attribute of ConditionalOnClass #8185
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
Conversation
When used as a meta-annotation the value() attribute of @ConditionalOnClass will fail silently resulting in the @conditional nature of the annotation being ignored.
Thanks for the detailed analysis. Rather than document this as an edge case I'd like to check if the core framework might be able to fix the underling issue. I've raise SPR-15311 |
The outcome from the framework is that this is going to be hard to fix. We'll just have to document the limitation. |
Ok cool, makes sense. Just FYI we need to also update the reference documentation to include the same/similar caveat, wasn't sure exactly where that resides. |
…ute of @ConditionalOnMissingClass
@philwebb this is good to go from my perspective. In the reference docs I went back and forth between moving the |
@phillipuniverse Thanks. Are you planning to submit a pull-request for these updates? |
@phillipuniverse Sorry ignore me, it's just been pointed out to me that this is a pull-request :) |
When used as a meta-annotation the value() attribute of @ConditionalOnClass will fail silently resulting in the @conditional nature of the annotation being ignored. See gh-8185
* pr/8185: Polish "Clarify edge case docs on ConditionalOnClass" Clarify edge case docs on ConditionalOnClass
When used as a meta-annotation the
value()
attribute of@ConditionalOnClass
will fail silently resulting in the@Conditional
nature of the annotation being ignored.Here is an example that fails silently:
On startup,
SomeComponentToBeOptionallyRegistered
will still be instantiated as a Spring component. The workaround is simple, simply add the@ConditionalOnClass
directly to the component:The reason that the first example fails is because while the annotations directly on the component are read via bytecode reading, meta-annotations are read via Java reflection which results in class loading (specifically, loading the
ClassThatMayNotExistAtRuntime
class). The location in which this fails is in Spring Core'sAnnotationUtils.getAnnotations(AnnotatedElement annotatedElement)
. This usesannotatedElement.getAnnotations()
, part of Java's reflection package, which triggers a load of theClassThatMayNotExistAtRuntime.class
, and then throws asun.reflect.annotation.TypeNotPresentExceptionProxy
. This ends up failing silently assuming that you do not have debug-logging enabled on this piece, specifically inhandleIntrospectionFailure()
.This was verified against Spring core 4.3.6.RELEASE and Spring Boot 1.5.1.RELEASE.