Takeaway: When building a Spring application, sometimes we want to load a bean into the application context only when a specified condition is met. Spring 4 provides a more general way to create Conditional beans using the @Conditional annotation. We can do this via @Conditional annotations.
@Conditional meta annotated source code
Conditional interface source code
We see that the @Conditional annotation takes a generic class of the extends Condition interface, that is, to use the @Conditional annotation, we simply need to implement the Condition interface and rewrite its methods:
Take a quick look at other examples in the Spring family that implement SpringBootCondition
The basis for all Condition implementations used by Spring Boot. Provide reasonable logging to help users diagnose classes to load.
I accidentally saw a vivid understanding on the Internet: If the Application Context is a hall, Spring Boot refines it on the basis of the @Conditional annotation without the need to present complex letters of introduction (implementing the Condition interface), Just hand your ticket with the pre-defined @conditionalonxxxx stamp and, if verified, walk into the Application Context lobby
SpringBoot Condition extension implementation
Opening each of the 13 annotations one by one, we find that they all have the same meta-annotation
From these markers we can learn the following:
-
Can be applied to TYPE, that is, Spring automatically scans all classes (@Configuration, @Component, @Service, @repository, Or@controller) can be checked by adding the corresponding @conditionalonxxxx
-
Can be applied to methods, so methods with the @bean tag can also apply these annotations
-
All of them are marked with @Conditional annotations. Custom conditions such as OnBeanCondition still implement the Condition interface. There is nothing mysterious about it, but they are encapsulated more specifically.
In fact, the usage is much the same, after looking at the source code found that the principle is very simple, here is a simple summary of usage:
In addition to customizing your own conditions, Spring provides a number of conditions for you to use
-
@ConditionalOnBean
A Bean is instantiated only if an object exists in the current context
-
@ConditionalOnClass
A Bean is instantiated only if a class is on the classpath
-
@ConditionalOnExpression
A Bean is instantiated only when the expression is true
-
@ConditionalOnMissingBean
A Bean is instantiated only if an object does not exist in the current context
-
@ConditionalOnMissingClass
A Bean is instantiated only if it does not exist on a class classpath
-
@ConditionalOnNotWebApplication
Not a Web application
-
@ConditionalOnProperty
If the property configured in the configuration is true, a Bean will be instantiated.
-
@ConditionalOnResource
We can use this annotation if the bean we are loading depends on whether or not the resource exists in the classpath
-
@ConditionalOnJndi
The bean is loaded only after the specified resource is loaded through JNDI
-
@ConditionalOnCloudPlatform
Only run on the specified cloud platform of the specified bean, CloudPlatform is org springframework. Boot. Under the cloud of an enum type class, everyone can open to see
Now that you’ve seen how to configure beans flexibly, I encourage you to open your IDE and review these annotations for more details. (Mutual encouragement: read the source code, learn daishen code design)
Do you know SpringBoot combination conditions?