- 编写Condition实现类
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); } }
- 编写Conditional注解类
@Documented @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD }) @Conditional(OnSystemPropertyCondition.class) public @interface ConditionalOnSystemProperty{ // Java 系统属性名称 String name() default "" ; // Java 系统属性值 String value() default "" ; }
- 业务代码添加Conditional注解
@ConditionalOnSystemProperty(name = "user.name", value = "yichengjie")//条件装配 public class HelloWorldAutoConfiguration { }
Springboot自定义Conditional学习笔记
猜你喜欢
转载自blog.csdn.net/yichengjie_c/article/details/114389039
今日推荐
周排行