Skip to content

Commit 7de8f4f

Browse files
committed
LazySingletonAspectInstanceFactoryDecorator uses shared singleton mutex
Issue: SPR-14241
1 parent cf39078 commit 7de8f4f

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 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.
@@ -18,9 +18,11 @@
1818

1919
import org.springframework.beans.factory.BeanFactory;
2020
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
21+
import org.springframework.beans.factory.support.AbstractBeanFactory;
2122
import org.springframework.core.Ordered;
2223
import org.springframework.core.annotation.AnnotationUtils;
2324
import org.springframework.core.annotation.Order;
25+
import org.springframework.util.Assert;
2426
import org.springframework.util.ClassUtils;
2527

2628
/**
@@ -66,7 +68,9 @@ public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name) {
6668
* @param name the name of the bean
6769
* @param type the type that should be introspected by AspectJ
6870
*/
69-
public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name, Class type) {
71+
public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name, Class<?> type) {
72+
Assert.notNull(beanFactory, "BeanFactory must not be null");
73+
Assert.notNull(name, "Bean name must not be null");
7074
this.beanFactory = beanFactory;
7175
this.name = name;
7276
this.aspectMetadata = new AspectMetadata(type, name);
@@ -78,18 +82,20 @@ public Object getAspectInstance() {
7882
}
7983

8084
public ClassLoader getAspectClassLoader() {
81-
if (this.beanFactory instanceof ConfigurableBeanFactory) {
82-
return ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader();
83-
}
84-
else {
85-
return ClassUtils.getDefaultClassLoader();
86-
}
85+
return (this.beanFactory instanceof ConfigurableBeanFactory ?
86+
((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader() :
87+
ClassUtils.getDefaultClassLoader());
8788
}
8889

8990
public AspectMetadata getAspectMetadata() {
9091
return this.aspectMetadata;
9192
}
9293

94+
public Object getAspectCreationMutex() {
95+
return (this.beanFactory instanceof AbstractBeanFactory ?
96+
((AbstractBeanFactory) this.beanFactory).getSingletonMutex() : this);
97+
}
98+
9399
/**
94100
* Determine the order for this factory's target aspect, either
95101
* 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-2009 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.
@@ -42,9 +42,13 @@ public LazySingletonAspectInstanceFactoryDecorator(MetadataAwareAspectInstanceFa
4242
}
4343

4444

45-
public synchronized Object getAspectInstance() {
45+
public Object getAspectInstance() {
4646
if (this.materialized == null) {
47-
synchronized (this) {
47+
Object mutex = this;
48+
if (this.maaif instanceof BeanFactoryAspectInstanceFactory) {
49+
mutex = ((BeanFactoryAspectInstanceFactory) this.maaif).getAspectCreationMutex();
50+
}
51+
synchronized (mutex) {
4852
if (this.materialized == null) {
4953
this.materialized = this.maaif.getAspectInstance();
5054
}

0 commit comments

Comments
 (0)