- 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); } }
- 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 "" ; }
- 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(); } }
Conditional study notes for Springboot conditional assembly
Guess you like
Origin blog.csdn.net/yichengjie_c/article/details/114165433
Ranking