Skip to content

Commit a1c0987

Browse files
committed
Avoid potential deadlock in AbstractBeanFactoryPointcutAdvisor
Issue: SPR-14388 (cherry picked from commit 0d3a22c)
1 parent 0b8822f commit a1c0987

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

spring-aop/src/main/java/org/springframework/aop/support/AbstractBeanFactoryPointcutAdvisor.java

+27-3
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.
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.beans.factory.BeanFactory;
2525
import org.springframework.beans.factory.BeanFactoryAware;
26+
import org.springframework.beans.factory.support.AbstractBeanFactory;
2627
import org.springframework.util.Assert;
2728

2829
/**
@@ -71,8 +72,23 @@ public String getAdviceBeanName() {
7172

7273
public void setBeanFactory(BeanFactory beanFactory) {
7374
this.beanFactory = beanFactory;
75+
resetAdviceMonitor();
7476
}
7577

78+
private void resetAdviceMonitor() {
79+
if (this.beanFactory instanceof AbstractBeanFactory) {
80+
this.adviceMonitor = ((AbstractBeanFactory) this.beanFactory).getSingletonMutex();
81+
}
82+
else {
83+
this.adviceMonitor = new Object();
84+
}
85+
}
86+
87+
/**
88+
* Specify a particular instance of the target advice directly,
89+
* avoiding lazy resolution in {@link #getAdvice()}.
90+
* @since 3.1
91+
*/
7692
public void setAdvice(Advice advice) {
7793
synchronized (this.adviceMonitor) {
7894
this.advice = advice;
@@ -91,7 +107,15 @@ public Advice getAdvice() {
91107

92108
@Override
93109
public String toString() {
94-
return getClass().getName() + ": advice bean '" + getAdviceBeanName() + "'";
110+
StringBuilder sb = new StringBuilder(getClass().getName());
111+
sb.append(": advice ");
112+
if (this.adviceBeanName != null) {
113+
sb.append("bean '").append(this.adviceBeanName).append("'");
114+
}
115+
else {
116+
sb.append(this.advice);
117+
}
118+
return sb.toString();
95119
}
96120

97121

@@ -104,7 +128,7 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound
104128
ois.defaultReadObject();
105129

106130
// Initialize transient fields.
107-
this.adviceMonitor = new Object();
131+
resetAdviceMonitor();
108132
}
109133

110134
}

0 commit comments

Comments
 (0)