Skip to content

Commit 68cdd5d

Browse files
committed
LazySingletonAspectInstanceFactoryDecorator uses shared singleton mutex
Issue: SPR-14241
1 parent bf3dee9 commit 68cdd5d

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

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

Lines changed: 12 additions & 7 deletions
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.
@@ -20,6 +20,7 @@
2020
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
2121
import org.springframework.core.Ordered;
2222
import org.springframework.core.annotation.OrderUtils;
23+
import org.springframework.util.Assert;
2324
import org.springframework.util.ClassUtils;
2425

2526
/**
@@ -66,6 +67,8 @@ public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name) {
6667
* @param type the type that should be introspected by AspectJ
6768
*/
6869
public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name, Class<?> type) {
70+
Assert.notNull(beanFactory, "BeanFactory must not be null");
71+
Assert.notNull(name, "Bean name must not be null");
6972
this.beanFactory = beanFactory;
7073
this.name = name;
7174
this.aspectMetadata = new AspectMetadata(type, name);
@@ -79,19 +82,21 @@ public Object getAspectInstance() {
7982

8083
@Override
8184
public ClassLoader getAspectClassLoader() {
82-
if (this.beanFactory instanceof ConfigurableBeanFactory) {
83-
return ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader();
84-
}
85-
else {
86-
return ClassUtils.getDefaultClassLoader();
87-
}
85+
return (this.beanFactory instanceof ConfigurableBeanFactory ?
86+
((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader() :
87+
ClassUtils.getDefaultClassLoader());
8888
}
8989

9090
@Override
9191
public AspectMetadata getAspectMetadata() {
9292
return this.aspectMetadata;
9393
}
9494

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

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 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.
@@ -43,9 +43,13 @@ public LazySingletonAspectInstanceFactoryDecorator(MetadataAwareAspectInstanceFa
4343

4444

4545
@Override
46-
public synchronized Object getAspectInstance() {
46+
public Object getAspectInstance() {
4747
if (this.materialized == null) {
48-
synchronized (this) {
48+
Object mutex = this;
49+
if (this.maaif instanceof BeanFactoryAspectInstanceFactory) {
50+
mutex = ((BeanFactoryAspectInstanceFactory) this.maaif).getAspectCreationMutex();
51+
}
52+
synchronized (mutex) {
4953
if (this.materialized == null) {
5054
this.materialized = this.maaif.getAspectInstance();
5155
}

0 commit comments

Comments
 (0)