Skip to content

Commit e6e3ca3

Browse files
committed
LazySingletonAspectInstanceFactoryDecorator uses shared singleton mutex
Issue: SPR-14241
1 parent e26478e commit e6e3ca3

File tree

6 files changed

+44
-14
lines changed

6 files changed

+44
-14
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -82,19 +82,22 @@ public Object getAspectInstance() {
8282

8383
@Override
8484
public ClassLoader getAspectClassLoader() {
85-
if (this.beanFactory instanceof ConfigurableBeanFactory) {
86-
return ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader();
87-
}
88-
else {
89-
return ClassUtils.getDefaultClassLoader();
90-
}
85+
return (this.beanFactory instanceof ConfigurableBeanFactory ?
86+
((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader() :
87+
ClassUtils.getDefaultClassLoader());
9188
}
9289

9390
@Override
9491
public AspectMetadata getAspectMetadata() {
9592
return this.aspectMetadata;
9693
}
9794

95+
@Override
96+
public Object getAspectCreationMutex() {
97+
return (this.beanFactory instanceof ConfigurableBeanFactory ?
98+
((ConfigurableBeanFactory) this.beanFactory).getSingletonMutex() : this);
99+
}
100+
98101
/**
99102
* Determine the order for this factory's target aspect, either
100103
* an instance-specific order expressed through implementing the

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -46,9 +46,9 @@ public LazySingletonAspectInstanceFactoryDecorator(MetadataAwareAspectInstanceFa
4646

4747

4848
@Override
49-
public synchronized Object getAspectInstance() {
49+
public Object getAspectInstance() {
5050
if (this.materialized == null) {
51-
synchronized (this) {
51+
synchronized (this.maaif.getAspectCreationMutex()) {
5252
if (this.materialized == null) {
5353
this.materialized = this.maaif.getAspectInstance();
5454
}
@@ -71,6 +71,11 @@ public AspectMetadata getAspectMetadata() {
7171
return this.maaif.getAspectMetadata();
7272
}
7373

74+
@Override
75+
public Object getAspectCreationMutex() {
76+
return this.maaif.getAspectCreationMutex();
77+
}
78+
7479
@Override
7580
public int getOrder() {
7681
return this.maaif.getOrder();

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -39,4 +39,11 @@ public interface MetadataAwareAspectInstanceFactory extends AspectInstanceFactor
3939
*/
4040
AspectMetadata getAspectMetadata();
4141

42+
/**
43+
* Return the best possible creation mutex for this factory.
44+
* @return the mutex object (never {@code null})
45+
* @since 4.3
46+
*/
47+
Object getAspectCreationMutex();
48+
4249
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -50,6 +50,11 @@ public final AspectMetadata getAspectMetadata() {
5050
return this.metadata;
5151
}
5252

53+
@Override
54+
public Object getAspectCreationMutex() {
55+
return this;
56+
}
57+
5358
@Override
5459
protected int getOrderForAspectClass(Class<?> aspectClass) {
5560
return OrderUtils.getOrder(aspectClass, Ordered.LOWEST_PRECEDENCE);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -55,6 +55,11 @@ public final AspectMetadata getAspectMetadata() {
5555
return this.metadata;
5656
}
5757

58+
@Override
59+
public Object getAspectCreationMutex() {
60+
return this;
61+
}
62+
5863
@Override
5964
protected int getOrderForAspectClass(Class<?> aspectClass) {
6065
return OrderUtils.getOrder(aspectClass, Ordered.LOWEST_PRECEDENCE);

spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactoryTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -693,6 +693,11 @@ public AspectMetadata getAspectMetadata() {
693693
return new AspectMetadata(PerTypeWithinAspect.class, "perTypeWithin");
694694
}
695695

696+
@Override
697+
public Object getAspectCreationMutex() {
698+
return this;
699+
}
700+
696701
@Override
697702
public int getOrder() {
698703
return Ordered.LOWEST_PRECEDENCE;

0 commit comments

Comments
 (0)