1. @ConditionalOnBean

When a bean exists in the IOC container

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnBean {

    // Type of the bean classClass<? >[] value()default {};

    // The type name of the bean class (classpath, for example: String is "java.lang.string")
    String[] type() default {};

    // The bean has the specified annotation
    Class<? extends Annotation>[] annotation() default {};

    // The bean name
    String[] name() default {};

    // Container level
    // ALL: ALL containers
    // ANCESTORS: all ancestor containers, but not the current one
    // CURRENT: indicates only the CURRENT container
    SearchStrategy search(a) default SearchStrategy.ALL;

    // 1.1 extends belowClass<? >[] parameterizedContainer()default {};

}
Copy the code

1.1 parameterizedContainer Extension:

  • Specify the type of the bean class: generic class

  • Matching rule: There is A bean named A whose type is the same as the generic class specified by parameterizedContainer (either an implementation class or A derived class) and whose generic parameter is the type of the bean annotated by @conditionAlonBean

  • A simple example:

    @Configuration
    public class TestConfiguration {
    
        // When the bean exists, Apple initializes it to the IOC container
        @Bean
        public Set<Apple> set1(a) {
            return new HashSet<>();
        }
    
        // Apple cannot initialize the bean to the IOC container while it exists
        @Bean
        public Set<Integer> set2(a) {
            return new HashSet<>();
        }
        
        // Apple cannot initialize the bean to the IOC container while it exists
        @Bean
        public List<Apple> list1(a) {
            return new ArrayList<>();
        }
    
        @Bean
        @ConditionalOnBean(parameterizedContainer = Set.class)
        public Apple apple(a) {
            return newApple(); }}Copy the code

2. @ConditionalOnMissingBean

A bean does not exist in the IOC container

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnMissingBean {

    // The type of bean class that can be ignoredClass<? >[] ignored()default {};

    // The type name of the bean class that can be ignored (classpath)
    String[] ignoredType() default {};

    // The other attributes are the same as @conditionalonbean

}
Copy the code

3. @ConditionalOnClass

A class exists in the classpath

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {

    // Type of the classClass<? >[] value()default {};

    // Class type name (classpath)
    String[] name() default {};

}
Copy the code

4. @ConditionalOnMissingClass

A class does not exist in the classpath

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnMissingClass {

    // Class type name (classpath)
    String[] value() default {};

}
Copy the code

5. @ConditionalOnWebApplication

Running in a Web application

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnWebApplication {

    // Type of Web application
    Type type(a) default Type.ANY;

    enum Type {

        // Any type
        ANY,

        / / traditional MVC
        SERVLET,

        / / response type
        REACTIVE

    }

}
Copy the code

6. @ConditionalOnNotWebApplication

Not running in a Web application

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnNotWebApplication {
    // No property field, indicating that it is not running in any Web application
}
Copy the code

7. @ConditionalOnProperty

When the corresponding configuration attribute meets the condition

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {

    // Complete the configuration name
    // Part of the configuration name, prefix + value is used together to complete the configuration name
    // Value and name cannot be used together
    String[] value() default {};

    // Configure the name prefix
    String prefix(a) default "";

    // Complete the configuration name
    Prefix + name Is used together to complete the configuration
    String[] name() default {};

    // Compare the value of the specified configuration attribute with that of havingValue
    String havingValue(a) default "";

    // The specified configuration attribute value does not exist
    // true: valid
    // false: does not take effect
    boolean matchIfMissing(a) default false;

}
Copy the code

8. @ConditionalOnResource

If the resource in the specified path exists

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnResourceCondition.class)
public @interface ConditionalOnResource {

    / / resource path, such as: "the classpath: meta-inf/services/javax.mail validation. The spi. ValidationProvider"
    String[] resources() default {};

}
Copy the code

9. @ConditionalOnSingleCandidate

Specifying that there is only one bean in the beanFactory, or that there are more than one, specifies the preferred (@primay) bean

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnSingleCandidate {

    // Type of the bean classClass<? > value()default Object.class;

    // The type name of the bean class (classpath)
    String type(a) default "";

    // Container level
    // ALL: ALL containers
    // ANCESTORS: all ancestor containers, but not the current one
    // CURRENT: indicates only the CURRENT container
    SearchStrategy search(a) default SearchStrategy.ALL;

}
Copy the code

10. @ConditionalOnCloudPlatform

When the specified cloud platform is activated

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnCloudPlatformCondition.class)
public @interface ConditionalOnCloudPlatform {

    // Cloud platform type
    CloudPlatform value(a);

}
Copy the code

11. @ConditionalOnExpression

If the specified SpEL expression is met

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnExpressionCondition.class)
public @interface ConditionalOnExpression {

    // SpEL expressions, e.g. "'${mq.comsumer}'. Equals ('rabbitmq')"
    // The default value "true" indicates that it takes effect
    String value(a) default "true";

}
Copy the code

12. @ConditionalOnJava

Runs on the specified version of the JVM

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnJavaCondition.class)
public @interface ConditionalOnJava {

    // Version matching degree
    Range range(a) default Range.EQUAL_OR_NEWER;

    / / Java version
    JavaVersion value(a);

    enum Range {

        // Equal to or later than the specified version
        EQUAL_OR_NEWER,

        // Earlier than the specified version
        OLDER_THAN

    }

}
Copy the code

13. @ConditionalOnJndi

After loading through JNDI

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnJndiCondition.class)
public @interface ConditionalOnJndi {

    // Specify a resource, for example: "Java :/JmsXA"
    // If one exists, the condition is satisfied
    String[] value() default {};

}
Copy the code

JNDI to understand