diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java b/spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java index 4ed2387649..12232bddf7 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java @@ -136,13 +136,8 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { private final XmlValidationModeDetector validationModeDetector = new XmlValidationModeDetector(); - private final ThreadLocal> resourcesCurrentlyBeingLoaded = - new NamedThreadLocal<>("XML bean definition resources currently being loaded"){ - @Override - protected Set initialValue() { - return new HashSet<>(4); - } - }; + private final ThreadLocal> resourcesCurrentlyBeingLoaded = NamedThreadLocal.withInitial( + "XML bean definition resources currently being loaded", () -> new HashSet<>(4)); /** diff --git a/spring-context/src/main/java/org/springframework/context/support/SimpleThreadScope.java b/spring-context/src/main/java/org/springframework/context/support/SimpleThreadScope.java index 7063bca874..00af013192 100644 --- a/spring-context/src/main/java/org/springframework/context/support/SimpleThreadScope.java +++ b/spring-context/src/main/java/org/springframework/context/support/SimpleThreadScope.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,7 +55,8 @@ public class SimpleThreadScope implements Scope { private static final Log logger = LogFactory.getLog(SimpleThreadScope.class); - private final ThreadLocal> threadScope = NamedThreadLocal.withInitial("SimpleThreadScope", HashMap::new); + private final ThreadLocal> threadScope = NamedThreadLocal.withInitial( + "SimpleThreadScope", HashMap::new); @Override public Object get(String name, ObjectFactory objectFactory) { diff --git a/spring-core/src/main/java/org/springframework/core/NamedThreadLocal.java b/spring-core/src/main/java/org/springframework/core/NamedThreadLocal.java index f90a572d80..63377639e6 100644 --- a/spring-core/src/main/java/org/springframework/core/NamedThreadLocal.java +++ b/spring-core/src/main/java/org/springframework/core/NamedThreadLocal.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,12 +48,11 @@ public class NamedThreadLocal extends ThreadLocal { /** * Creates a named thread local variable. The initial value of the variable is * determined by invoking the {@code get} method on the {@code Supplier}. - * * @param the type of the named thread local's value + * @param name a descriptive name for the thread local * @param supplier the supplier to be used to determine the initial value - * @return a new named thread local variable - * @throws NullPointerException if the specified supplier is null - * @since 5.2.5 + * @return a new named thread local + * @since 6.1 */ public static ThreadLocal withInitial(String name, Supplier supplier) { return new SuppliedNamedThreadLocal<>(name, supplier); @@ -83,6 +82,7 @@ public class NamedThreadLocal extends ThreadLocal { protected T initialValue() { return this.supplier.get(); } + } }