Skip to content

Commit

Permalink
Use SingletonSupplier for XStream instance in XStreamMarshaller
Browse files Browse the repository at this point in the history
Closes gh-25017
  • Loading branch information
sbrannen committed May 7, 2020
1 parent ce11fdf commit 4805288
Showing 1 changed file with 6 additions and 16 deletions.
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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> xstream = SingletonSupplier.of(this::buildXStream);


/**
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -617,21 +617,11 @@ protected void customizeXStream(XStream xstream) {
* <p><b>NOTE: This method has been marked as final as of Spring 4.0.</b>
* It can be used to access the fully configured XStream for marshalling
* but not configuration purposes anymore.
* <p>As of Spring Framework 5.2.7, creation of the {@link XStream} instance
* <p>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();
}


Expand Down

0 comments on commit 4805288

Please sign in to comment.