From 343760fadbc7f12c70ac3e21e2385e708cf2b417 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 7 May 2020 12:35:32 +0200 Subject: [PATCH] Use SingletonSupplier for XStream instance in XStreamMarshaller Closes gh-25017 --- .../oxm/xstream/XStreamMarshaller.java | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java index 596b587fa292..090ae9f167ff 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java @@ -84,6 +84,7 @@ import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; +import org.springframework.util.function.SingletonSupplier; import org.springframework.util.xml.StaxUtils; /** @@ -187,8 +188,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo private ClassLoader beanClassLoader = new CompositeClassLoader(); - @Nullable - private volatile XStream xstream; + private final SingletonSupplier xstream = SingletonSupplier.of(this::buildXStream); /** @@ -407,12 +407,12 @@ public void setBeanClassLoader(ClassLoader classLoader) { @Override public void afterPropertiesSet() { - this.xstream = buildXStream(); + // no-op due to use of SingletonSupplier for the XStream field. } /** * Build the native XStream delegate to be used by this marshaller, - * delegating to {@link #constructXStream()}, {@link #configureXStream} + * delegating to {@link #constructXStream}, {@link #configureXStream}, * and {@link #customizeXStream}. */ protected XStream buildXStream() { @@ -617,21 +617,11 @@ protected void customizeXStream(XStream xstream) { *

NOTE: This method has been marked as final as of Spring 4.0. * It can be used to access the fully configured XStream for marshalling * but not configuration purposes anymore. - *

As of Spring Framework 5.2.7, creation of the {@link XStream} instance + *

As of Spring Framework 5.1.16, creation of the {@link XStream} instance * returned by this method is thread safe. */ public final XStream getXStream() { - XStream xs = this.xstream; - if (xs == null) { - synchronized (this) { - xs = this.xstream; - if (xs == null) { - xs = buildXStream(); - this.xstream = xs; - } - } - } - return xs; + return this.xstream.obtain(); }