Conditional study notes for Springboot conditional assembly

  1. Write the Condition implementation class (refer to: OnPropertyCondition)
    public class OnSystemPropertyCondition  implements Condition {
          
          
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
          
          
            Map<String, Object> attributes =
                    metadata.getAnnotationAttributes(ConditionalOnSystemProperty.class.getName());
            String propertyName = (String) attributes.get("name") ;
            String propertyValue = (String) attributes.get("value") ;
            String systemPropertyValue = System.getProperty(propertyName) ;
            return propertyValue.equalsIgnoreCase(systemPropertyValue);
        }
    }
    
  2. Write Conditional annotation classes (refer to: ConditionalOnProperty)
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({
          
           ElementType.TYPE, ElementType.METHOD })
    @Conditional(OnSystemPropertyCondition.class)
    public @interface ConditionalOnSystemProperty{
          
          
        // Java 系统属性名称
        String name() default "" ;
        // Java 系统属性值
        String value() default "" ;
    }
    
  3. Business and startup class writing
    @Slf4j
    public class ConditionalOnSystemPropertyApplication {
          
          
        @Bean
        @ConditionalOnSystemProperty(name = "user.name", value = "yichengjie")
        public String helloWorld(){
          
          
            return "Hello, yicj" ;
        }
        public static void main(String[] args) {
          
          
            ConfigurableApplicationContext ctx = new SpringApplicationBuilder(ConditionalOnSystemPropertyApplication.class)
                    .web(WebApplicationType.NONE)
                    .run(args);
            String bean = ctx.getBean("helloWorld", String.class);
            log.info("====> bean : {}", bean);
            ctx.close();
        }
    }
    

Guess you like

Origin blog.csdn.net/yichengjie_c/article/details/114165433