
class OnWebApplicationCondition extends SpringBootCondition { private static final String WEB_CONTEXT_CLASS = "org.springframework.web.context." + "support.GenericWebApplicationContext"; @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { boolean required = metadata .isAnnotated(ConditionalOnWebApplication.class.getName()); ConditionOutcome outcome = isWebApplication(context, metadata, required); if (required && !outcome.isMatch()) { return ConditionOutcome.noMatch(outcome.getConditionMessage()); } if (!required && outcome.isMatch()) { return ConditionOutcome.noMatch(outcome.getConditionMessage()); } return ConditionOutcome.match(outcome.getConditionMessage()); } private ConditionOutcome isWebApplication(ConditionContext context, AnnotatedTypeMetadata metadata, boolean required) { switch (deduceType(metadata)) { case SERVLET: return isServletWebApplication(context); case REACTIVE: return isReactiveWebApplication(context); default: return isAnyWebApplication(context, required); } } 在 Spring 源码中,经常看到类似于 getMatchOutcome()方法调用 isWebApplication()方法,并且 isWebApplication()方法紧跟在调用它的位置下方的代码顺序。 这种代码顺序的选择是基于团队良好的代码风格还是通过工具进行自动调整的?另外想了解有没有代码风格的最佳实践以及优化代码顺序的相关工具? 1 Rache1 2023 年 10 月 24 日 |