Skip to content

Commit 99ed01e

Browse files
committed
Optimize locking in AspectJProxyFactory for concurrent aspect instantiation
Closes gh-26034
1 parent 2657757 commit 99ed01e

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -160,23 +160,12 @@ private MetadataAwareAspectInstanceFactory createAspectInstanceFactory(
160160
}
161161

162162
/**
163-
* Get the singleton aspect instance for the supplied aspect type. An instance
164-
* is created if one cannot be found in the instance cache.
163+
* Get the singleton aspect instance for the supplied aspect type.
164+
* An instance is created if one cannot be found in the instance cache.
165165
*/
166166
private Object getSingletonAspectInstance(Class<?> aspectClass) {
167-
// Quick check without a lock...
168-
Object instance = aspectCache.get(aspectClass);
169-
if (instance == null) {
170-
synchronized (aspectCache) {
171-
// To be safe, check within full lock now...
172-
instance = aspectCache.get(aspectClass);
173-
if (instance == null) {
174-
instance = new SimpleAspectInstanceFactory(aspectClass).getAspectInstance();
175-
aspectCache.put(aspectClass, instance);
176-
}
177-
}
178-
}
179-
return instance;
167+
return aspectCache.computeIfAbsent(aspectClass,
168+
clazz -> new SimpleAspectInstanceFactory(clazz).getAspectInstance());
180169
}
181170

182171

0 commit comments

Comments
 (0)