Browse Source

Polish "Add factory to create a NamedThreadLocal with an initial value"

See gh-24705
pull/31113/head
Stephane Nicoll 2 years ago
parent
commit
5878a0741e
  1. 9
      spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java
  2. 5
      spring-context/src/main/java/org/springframework/context/support/SimpleThreadScope.java
  3. 10
      spring-core/src/main/java/org/springframework/core/NamedThreadLocal.java

9
spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java

@ -136,13 +136,8 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { @@ -136,13 +136,8 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
private final XmlValidationModeDetector validationModeDetector = new XmlValidationModeDetector();
private final ThreadLocal<Set<EncodedResource>> resourcesCurrentlyBeingLoaded =
new NamedThreadLocal<>("XML bean definition resources currently being loaded"){
@Override
protected Set<EncodedResource> initialValue() {
return new HashSet<>(4);
}
};
private final ThreadLocal<Set<EncodedResource>> resourcesCurrentlyBeingLoaded = NamedThreadLocal.withInitial(
"XML bean definition resources currently being loaded", () -> new HashSet<>(4));
/**

5
spring-context/src/main/java/org/springframework/context/support/SimpleThreadScope.java

@ -1,5 +1,5 @@ @@ -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 { @@ -55,7 +55,8 @@ public class SimpleThreadScope implements Scope {
private static final Log logger = LogFactory.getLog(SimpleThreadScope.class);
private final ThreadLocal<Map<String, Object>> threadScope = NamedThreadLocal.withInitial("SimpleThreadScope", HashMap::new);
private final ThreadLocal<Map<String, Object>> threadScope = NamedThreadLocal.withInitial(
"SimpleThreadScope", HashMap::new);
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {

10
spring-core/src/main/java/org/springframework/core/NamedThreadLocal.java

@ -1,5 +1,5 @@ @@ -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<T> extends ThreadLocal<T> { @@ -48,12 +48,11 @@ public class NamedThreadLocal<T> extends ThreadLocal<T> {
/**
* 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 <S> 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 <S> ThreadLocal<S> withInitial(String name, Supplier<? extends S> supplier) {
return new SuppliedNamedThreadLocal<>(name, supplier);
@ -83,6 +82,7 @@ public class NamedThreadLocal<T> extends ThreadLocal<T> { @@ -83,6 +82,7 @@ public class NamedThreadLocal<T> extends ThreadLocal<T> {
protected T initialValue() {
return this.supplier.get();
}
}
}

Loading…
Cancel
Save