1,@Conditional brief introduction

@Conditional Annotations indicate that a component is eligible to register only if all specified conditions match .@Conditional Annotated value Attribute is condition matching Class object , Specifically speaking, it is Condition Interface instance array .
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME) @Documented public @interface Conditional {
// All conditions that must be matched to register a component Class<? extends Condition>[] value(); }
@Conditional Annotations can be used in any of the following ways :

* For any direct or indirect use @Component The type level annotation of the annotated class , include @Configuration class
* As a meta comment , Used to write custom stereotype comments
* As any @Bean Method level annotation for methods

If @Configuration Class marked as @Conditional, Then all the @Bean method ,@Import Notes and @ComponentScan Comments are subject to these conditions .

Let's take a look at the interface Condition The definition of .
@FunctionalInterface public interface Condition { // Determine whether the conditions match . boolean
matches(ConditionContext context, AnnotatedTypeMetadata metadata); }
2, Principle analysis

stay SpringFramework in @Conditional The processor behind the annotation is ConditionEvaluator, Through tracking the source code, we found that there are three uses ConditionEvaluator:

*
ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(BeanMethod)
analysis @Bean method
* ConfigurationClassParser.processConfigurationClass(ConfigurationClass)
analysis @Configuration class
* ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClass,
SourceClass) The analysis of being @ComponentScan Annotated class

They are used in three places ConditionEvaluator It works the same way ,ConditionEvaluator Of shouldSkip() Method returns a boolean value ,true Represents skipping the registration or parsing of the annotated component , Here is the implementation of the second one .

ConditionEvaluator The realization of .
public boolean shouldSkip(@Nullable AnnotatedTypeMetadata metadata, @Nullable
ConfigurationPhase phase) { if (metadata == null ||
!metadata.isAnnotated(Conditional.class.getName())) { return false; } if (phase
== null) { if (metadata instanceof AnnotationMetadata &&
ConfigurationClassUtils.isConfigurationCandidate((AnnotationMetadata)
metadata)) { return shouldSkip(metadata,
ConfigurationPhase.PARSE_CONFIGURATION); } return shouldSkip(metadata,
ConfigurationPhase.REGISTER_BEAN); } List<Condition> conditions = new
ArrayList<>(); for (String[] conditionClasses : getConditionClasses(metadata))
{ for (String conditionClass : conditionClasses) { Condition condition =
getCondition(conditionClass, this.context.getClassLoader());
conditions.add(condition); } } AnnotationAwareOrderComparator.sort(conditions);
for (Condition condition : conditions) { ConfigurationPhase requiredPhase =
null; if (condition instanceof ConfigurationCondition) { requiredPhase =
((ConfigurationCondition) condition).getConfigurationPhase(); }
// There is one condition return false Just skip class resolution or registration if ((requiredPhase == null || requiredPhase ==
phase) && !condition.matches(this.context, metadata)) { return true; } } return
false; }
 

 

Technology
©2019-2020 Toolsou All rights reserved,
tkinter Implementation of user login interface Vue Invalid render dynamic component in subcomponent ( First invalid , Valid for the second time )9 ride 9 Sudoku C/C++ Memory model TypeScript actual combat -12-TS The mechanism of type checking - Type inference Java Thread safety and insecurity JLink,STLink,DAPLink,CMSIS DAP Differences in use Chapter 10 use SpringMVC Frame transformation of supermarket order management system -1C Language calculation of deposit interest About the Blue Bridge Cup , Things you should know !