(1). RoutePredicateFactory类结构图
(2). RoutePredicateFactory
public interface RoutePredicateFactory<C>
extends ShortcutConfigurable, Configurable<C> {
// 获取实现了:RoutePredicateFactory类的名称
// 并删除后缀:RoutePredicateFactory
default String name() {
// BetweenRoute
return NameUtils.normalizeRoutePredicateName(getClass());
}
Predicate<ServerWebExchange> apply(C config);
}
(3). BetweenRoutePredicateFactory
public class BetweenRoutePredicateFactory
extends AbstractRoutePredicateFactory<BetweenRoutePredicateFactory.Config> {
public static final String DATETIME1_KEY = "datetime1";
public static final String DATETIME2_KEY = "datetime2";
public BetweenRoutePredicateFactory() {
super(Config.class);
}
public Predicate<ServerWebExchange> apply(Config config) {
ZonedDateTime datetime1 = config.datetime1;
ZonedDateTime datetime2 = config.datetime2;
Assert.isTrue(datetime1.isBefore(datetime2),
config.datetime1 +
" must be before " + config.datetime2);
// JDK8语法,创建: Predicate
return exchange -> {
final ZonedDateTime now = ZonedDateTime.now();
return now.isAfter(datetime1) && now.isBefore(datetime2);
};
} // end apply
}
(4). 总结
RoutePredicateFactory是RoutePredicate的工厂类,负责创建:RoutePredicate.