Browse Source

Use Map::computeIfAbsent to simplify scope implementations

Closes gh-25038
pull/25045/head
Yanming Zhou 6 years ago committed by GitHub
parent
commit
50a4fdac6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      spring-context/src/main/java/org/springframework/context/support/SimpleThreadScope.java
  2. 7
      spring-tx/src/main/java/org/springframework/transaction/support/SimpleTransactionScope.java

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

@ -67,12 +67,7 @@ public class SimpleThreadScope implements Scope { @@ -67,12 +67,7 @@ public class SimpleThreadScope implements Scope {
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
Map<String, Object> scope = this.threadScope.get();
Object scopedObject = scope.get(name);
if (scopedObject == null) {
scopedObject = objectFactory.getObject();
scope.put(name, scopedObject);
}
return scopedObject;
return scope.computeIfAbsent(name, k -> objectFactory.getObject());
}
@Override

7
spring-tx/src/main/java/org/springframework/transaction/support/SimpleTransactionScope.java

@ -50,12 +50,7 @@ public class SimpleTransactionScope implements Scope { @@ -50,12 +50,7 @@ public class SimpleTransactionScope implements Scope {
TransactionSynchronizationManager.registerSynchronization(new CleanupSynchronization(scopedObjects));
TransactionSynchronizationManager.bindResource(this, scopedObjects);
}
Object scopedObject = scopedObjects.scopedInstances.get(name);
if (scopedObject == null) {
scopedObject = objectFactory.getObject();
scopedObjects.scopedInstances.put(name, scopedObject);
}
return scopedObject;
return scopedObjects.scopedInstances.computeIfAbsent(name, k -> objectFactory.getObject());
}
@Override

Loading…
Cancel
Save