spring Conditional是怎麼生效的

平時看spring源碼中經常用到Conditional這個註解,今天來看一下他是怎麼生效的

  1. 其實我是發現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
	}
  1. 接着發現shouldSkip方法是在ConfigurationClassParser類的processConfigurationClass方法中進行了調用org.springframework.context.annotation.ConfigurationClassParser#processConfigurationClass

回到ConfigurationClassParser這個類就比較好理解,說明還是在解析配置階段根據Conditional註解進行處理的
ConfigurationClassParser 解析配置流程,點擊查看

參考文檔中對Conditional註解有更詳細的講解

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章