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 .config .ConfigurableBeanFactory ;
26
27
import org .springframework .util .Assert ;
27
28
28
29
/**
@@ -72,8 +73,23 @@ public String getAdviceBeanName() {
72
73
@ Override
73
74
public void setBeanFactory (BeanFactory beanFactory ) {
74
75
this .beanFactory = beanFactory ;
76
+ resetAdviceMonitor ();
75
77
}
76
78
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
+ */
77
93
public void setAdvice (Advice advice ) {
78
94
synchronized (this .adviceMonitor ) {
79
95
this .advice = advice ;
@@ -93,7 +109,15 @@ public Advice getAdvice() {
93
109
94
110
@ Override
95
111
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 ();
97
121
}
98
122
99
123
@@ -106,7 +130,7 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound
106
130
ois .defaultReadObject ();
107
131
108
132
// Initialize transient fields.
109
- this . adviceMonitor = new Object ();
133
+ resetAdviceMonitor ();
110
134
}
111
135
112
136
}
0 commit comments