1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
24
24
import org .springframework .beans .factory .BeanFactory ;
25
25
import org .springframework .beans .factory .BeanFactoryAware ;
26
+ import org .springframework .beans .factory .support .AbstractBeanFactory ;
26
27
import org .springframework .util .Assert ;
27
28
28
29
/**
@@ -71,8 +72,23 @@ public String getAdviceBeanName() {
71
72
72
73
public void setBeanFactory (BeanFactory beanFactory ) {
73
74
this .beanFactory = beanFactory ;
75
+ resetAdviceMonitor ();
74
76
}
75
77
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
+ */
76
92
public void setAdvice (Advice advice ) {
77
93
synchronized (this .adviceMonitor ) {
78
94
this .advice = advice ;
@@ -91,7 +107,15 @@ public Advice getAdvice() {
91
107
92
108
@ Override
93
109
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 ();
95
119
}
96
120
97
121
@@ -104,7 +128,7 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound
104
128
ois .defaultReadObject ();
105
129
106
130
// Initialize transient fields.
107
- this . adviceMonitor = new Object ();
131
+ resetAdviceMonitor ();
108
132
}
109
133
110
134
}
0 commit comments