平時看spring源碼中經常用到Conditional這個註解,今天來看一下他是怎麼生效的
- 其實我是發現Conditional註解是在ConditionEvaluator類的shouldSkip方法中調用了
org.springframework.context.annotation.ConditionEvaluator#shouldSkip
shouldSkip方法有個枚舉類型的參數ConfigurationPhase,PARSE_CONFIGURATION
是標示配置解析階段;REGISTER_BEAN
標示配置註冊階段
/**
* The various configuration phases where the condition could be evaluated.
*/
enum ConfigurationPhase {
/**
* The {@link Condition} should be evaluated as a {@code @Configuration}
* class is being parsed.
* <p>If the condition does not match at this point, the {@code @Configuration}
* class will not be added.
*/
PARSE_CONFIGURATION,
/**
* The {@link Condition} should be evaluated when adding a regular
* (non {@code @Configuration}) bean. The condition will not prevent
* {@code @Configuration} classes from being added.
* <p>At the time that the condition is evaluated, all {@code @Configuration}s
* will have been parsed.
*/
REGISTER_BEAN
}
- 接着發現shouldSkip方法是在ConfigurationClassParser類的processConfigurationClass方法中進行了調用
org.springframework.context.annotation.ConfigurationClassParser#processConfigurationClass
回到ConfigurationClassParser
這個類就比較好理解,說明還是在解析配置階段根據Conditional
註解進行處理的
ConfigurationClassParser 解析配置流程,點擊查看