Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

@Conditional Conditional assembly

@Conditional provides constraints for automatic assembly and is usually used in conjunction with @Configuration and @bean

Determine whether to implement Bean assembly based on the conditions of the annotation configuration

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

   /**
    * All {@link Condition Conditions} that must {@linkplain Condition#matches match}
    * in order for the component to be registered.
    */
   Class<? extends Condition>[] value();

}
Copy the code

Condition is an interface that provides the matches method, which returns true to indicate that the Bean can be injected and false to indicate that the Condition is not met and the Bean cannot be injected

Custom Condition

ConditionConfig

@Configuration
public class ConditionConfig {

    @Bean
    @Conditional(XpCondition.class)
    public BeanClass beanClass(a){
        return newBeanClass(); }}Copy the code
public class XpCondition implements Condition{

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        If true is returned, the configuration class or Bean needs to be loaded
        // Otherwise, it is loaded
        String os=conditionContext.getEnvironment().getProperty("os.name");
        if(os.contains("Linux")) {return true;
        }
        return false; }}Copy the code
public class ConditionMain {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context=newAnnotationConfigApplicationContext(ConditionConfig.class); BeanClass beanClass=context.getBean(BeanClass.class); System.out.println(beanClass); }}Copy the code

The BeanClass is loaded into the Spring Ioc container only when XpCondition’s matches returns true

SpringBoot extends Conditional:

  • ConditionalOnBean ConditionalOnMissingBean: ConditionalOnMissingBean is loaded when a class or Bean is not present in the container
  • ConditionalOnClass ConditionalOnMissingClass: under the classpath specified class or Bean loading when there is no specified class
  • ConditionalOnResource: Specifies whether the resource exists under classpath
  • ConditionalOnProperty: Specifies whether the corresponding attribute in the system has a value.

Batch automatic assembly condition configuration

SpringBoot provides spring-autoconfigure-metadata.properties to implement batch automatic assembly condition configuration

Convention over Configuration:

  • The configuration file path and name must be meta-INF /spring-autoconfigure-metadata.properties
  • The configuration format of the key in the configuration file is as follows: Class full path name of the automatic configuration class. Conditions = value

Reduces SpringBoot startup time, filtering occurs before configuration classes are assembled, and reduces the time it takes to load beans during SpringBoot startup.