首页 > Spring中ConfigurationCondition接口是干啥的?

Spring中ConfigurationCondition接口是干啥的?

org.springframework.context.annotation.ConfigurationCondition集成了org.springframework.context.annotation.Condition接口。Condition主要是用来带条件的自动装配的。

请问ConfigurationCondition接口是干啥的?

官方的接口申明如下:

package org.springframework.context.annotation;
 
/**
 * A {@link Condition} that offers more fine-grained control when used with
 * {@code @Configuration}. Allows certain {@link Condition}s to adapt when they match
 * based on the configuration phase. For example, a condition that checks if a bean
 * has already been registered might choose to only be evaluated during the
 * {@link ConfigurationPhase#REGISTER_BEAN REGISTER_BEAN} {@link ConfigurationPhase}.
 *
 * @author Phillip Webb
 * @since 4.0
 * @see Configuration
 */
public interface ConfigurationCondition extends Condition {
 
    /**
     * Return the {@link ConfigurationPhase} in which the condition should be evaluated.
     */
    ConfigurationPhase getConfigurationPhase();
 
 
    /**
     * The various configuration phases where the condition could be evaluated.
     */
    public static 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
    }
 
}

Condition可以用于判断只有当某个Bean已注册的情况下才进行Configuration,大部分情况下直接用Condition就OK了。ConfigurationCondition只是在Condition上面加了一个东西,就是什么时候做这个判断,也就是代码里的PARSE_CONFIGURATIONREGISTER_BEAN,具体啥意思参加这两个枚举的注释了

【热门文章】
【热门文章】