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.
2323
2424import org .springframework .beans .factory .BeanFactory ;
2525import org .springframework .beans .factory .BeanFactoryAware ;
26+ import org .springframework .beans .factory .support .AbstractBeanFactory ;
2627import 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