Skip to content

Commit fd9d518

Browse files
committed
Avoid potential deadlock in AbstractBeanFactoryPointcutAdvisor
Issue: SPR-14388
1 parent a2aa82e commit fd9d518

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.config.ConfigurableBeanFactory;
2627
import org.springframework.util.Assert;
2728

2829
/**
@@ -72,8 +73,23 @@ public String getAdviceBeanName() {
7273
@Override
7374
public void setBeanFactory(BeanFactory beanFactory) {
7475
this.beanFactory = beanFactory;
76+
resetAdviceMonitor();
7577
}
7678

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

94110
@Override
95111
public String toString() {
96-
return getClass().getName() + ": advice bean '" + getAdviceBeanName() + "'";
112+
StringBuilder sb = new StringBuilder(getClass().getName());
113+
sb.append(": advice ");
114+
if (this.adviceBeanName != null) {
115+
sb.append("bean '").append(this.adviceBeanName).append("'");
116+
}
117+
else {
118+
sb.append(this.advice);
119+
}
120+
return sb.toString();
97121
}
98122

99123

@@ -106,7 +130,7 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound
106130
ois.defaultReadObject();
107131

108132
// Initialize transient fields.
109-
this.adviceMonitor = new Object();
133+
resetAdviceMonitor();
110134
}
111135

112136
}

0 commit comments

Comments
 (0)