How does Conditional annotation works in Spring Boot?
Spring Boot is being compiled with lots of optional dependencies; so when Spring Boot is compiled, the MyBean.class
is on the classpath.
Now your application may not have that MyBean.class
in its classpath, but it doesn't fail at runtime. This is because the infrastructure that processes @ConditionalOnClass
annotations will actually read the bytecode of the configuration and will only load them if that MyBean.class
is present. See @ConditionalOnClass
javadoc.
Now auto-configuration is a broad subject, and you can learn more about this in this talk.
As the Spring Boot Documentation says:
The @ConditionalOnClass and @ConditionalOnMissingClass annotations allows configuration to be included based on the presence or absence of specific classes. Due to the fact that annotation metadata is parsed using ASM you can actually use the value attribute to refer to the real class, even though that class might not actually appear on the running application classpath. You can also use the name attribute if you prefer to specify the class name using a String value.
So they use the bytecode manipulation library ASM to be able to parse the class names during runtime, even if the classes are not anymore on the classpath. Since Spring is open source, you can even just go look at the annotation reading code.
You would typically use Optional
dependencies:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<optional>true</optional>
</dependency>