考虑下面的代码片段-将简单而不是线程安全的HashMap传递给SomeHandlerFunction
将在其他自动装配bean中使用的HashMap是否安全SomeHandlerFunction
?
@Bean
RouterFunction<ServerResponse> request(SomeHandlerFunction handlerFunction) {
handlerFunction.setSimpleMapProperties(new HashMap());
return route(GET("/dummy"), handlerFunction);
}
@Service
class SomeHandlerFunction implements HandlerFunction {
@Autowired
List<AnotherBeans> anotherBeans;
Mono<T> handle(ServerRequest var1) {
// code...
}
}
我对WebFlux中的多线程模型有所了解,但是这种情况使我感到困惑。
如果那HashMap
是可变的,那不是一个好主意-即使使用Spring MVC也不是。您应该怎么做呢?这实际上取决于手边的用例。
如果您要设置本地内存缓存,则可以使用Caffeine之类的库,也可以仅使用一个库ConcurrentMapCache
。
如果您想与该bean共享配置密钥,则可以使用Collections.unmodifiableMap
甚至更好的包装该映射,以使其成为适当的不可变POJO。
如果这是为了携带与正在处理的请求链接的临时数据,则应该使用请求属性或Reactor Context。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句