我看过java文档,但我不知道主要的使用场景
这里是文档;
/**
* Share a {@link Mono} for the duration of a function that may transform it and
* consume it as many times as necessary without causing multiple subscriptions
* to the upstream.
*
* @param transform the transformation function
* @param <R> the output value type
*
* @return a new {@link Mono}
*/
public final <R> Mono<R> publish(Function<? super Mono<T>, ? extends Mono<? extends
R>> transform) {
return onAssembly(new MonoPublishMulticast<>(this, transform));
}
我尝试了下面的单元测试代码片段
@Test
public void publish(){
Mono<String> publish = Mono.just("test")
.doOnNext(s -> log.info("publish --- {}",s))
.publish(mo -> mo.map(e->e+"add String"))
.doOnNext(s -> log.info("publish --- {}",s))
;
Mono<String> map = Mono.just("test")
.doOnNext(s -> log.info("publish --- {}", s))
.publish(mo -> mo.map(e -> e + "add String"))
.map(s -> s + "add String");
}
publish
似乎与 map
没有区别,它们的用法都是将指定对象转换为同一流中的另一种类型或值