2525import org .springframework .beans .BeanInstantiationException ;
2626import org .springframework .beans .BeanUtils ;
2727import org .springframework .beans .factory .BeanFactory ;
28-
2928import org .springframework .cglib .core .SpringNamingPolicy ;
3029import org .springframework .cglib .proxy .Callback ;
3130import org .springframework .cglib .proxy .CallbackFilter ;
3433import org .springframework .cglib .proxy .MethodInterceptor ;
3534import org .springframework .cglib .proxy .MethodProxy ;
3635import org .springframework .cglib .proxy .NoOp ;
36+ import org .springframework .util .StringUtils ;
3737
3838/**
3939 * Default object instantiation strategy for use in BeanFactories.
@@ -89,14 +89,13 @@ protected Object instantiateWithMethodInjection(RootBeanDefinition beanDefinitio
8989 */
9090 private static class CglibSubclassCreator {
9191
92- private static final Class <?>[] CALLBACK_TYPES = new Class <?>[] { NoOp . class ,
93- LookupOverrideMethodInterceptor .class , ReplaceOverrideMethodInterceptor .class };
92+ private static final Class <?>[] CALLBACK_TYPES = new Class <?>[]
93+ { NoOp . class , LookupOverrideMethodInterceptor .class , ReplaceOverrideMethodInterceptor .class };
9494
9595 private final RootBeanDefinition beanDefinition ;
9696
9797 private final BeanFactory owner ;
9898
99-
10099 CglibSubclassCreator (RootBeanDefinition beanDefinition , BeanFactory owner ) {
101100 this .beanDefinition = beanDefinition ;
102101 this .owner = owner ;
@@ -113,7 +112,6 @@ private static class CglibSubclassCreator {
113112 */
114113 Object instantiate (Constructor <?> ctor , Object [] args ) {
115114 Class <?> subclass = createEnhancedSubclass (this .beanDefinition );
116-
117115 Object instance ;
118116 if (ctor == null ) {
119117 instance = BeanUtils .instantiate (subclass );
@@ -123,19 +121,17 @@ Object instantiate(Constructor<?> ctor, Object[] args) {
123121 Constructor <?> enhancedSubclassConstructor = subclass .getConstructor (ctor .getParameterTypes ());
124122 instance = enhancedSubclassConstructor .newInstance (args );
125123 }
126- catch (Exception e ) {
124+ catch (Exception ex ) {
127125 throw new BeanInstantiationException (this .beanDefinition .getBeanClass (), String .format (
128- "Failed to invoke construcor for CGLIB enhanced subclass [%s]" , subclass .getName ()), e );
126+ "Failed to invoke constructor for CGLIB enhanced subclass [%s]" , subclass .getName ()), ex );
129127 }
130128 }
131-
132129 // SPR-10785: set callbacks directly on the instance instead of in the
133130 // enhanced class (via the Enhancer) in order to avoid memory leaks.
134131 Factory factory = (Factory ) instance ;
135- factory .setCallbacks (new Callback [] { NoOp .INSTANCE ,//
136- new LookupOverrideMethodInterceptor (beanDefinition , owner ),//
137- new ReplaceOverrideMethodInterceptor (beanDefinition , owner ) });
138-
132+ factory .setCallbacks (new Callback [] {NoOp .INSTANCE ,
133+ new LookupOverrideMethodInterceptor (this .beanDefinition , this .owner ),
134+ new ReplaceOverrideMethodInterceptor (this .beanDefinition , this .owner )});
139135 return instance ;
140136 }
141137
@@ -153,6 +149,7 @@ private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
153149 }
154150 }
155151
152+
156153 /**
157154 * Class providing hashCode and equals methods required by CGLIB to
158155 * ensure that CGLIB doesn't generate a distinct class per bean.
@@ -162,7 +159,6 @@ private static class CglibIdentitySupport {
162159
163160 private final RootBeanDefinition beanDefinition ;
164161
165-
166162 CglibIdentitySupport (RootBeanDefinition beanDefinition ) {
167163 this .beanDefinition = beanDefinition ;
168164 }
@@ -173,8 +169,8 @@ RootBeanDefinition getBeanDefinition() {
173169
174170 @ Override
175171 public boolean equals (Object other ) {
176- return other . getClass ().equals (this .getClass ())
177- && (( CglibIdentitySupport ) other ).getBeanDefinition (). equals ( this . getBeanDefinition ( ));
172+ return ( getClass ().equals (other .getClass ()) &&
173+ this . beanDefinition . equals ((( CglibIdentitySupport ) other ).beanDefinition ));
178174 }
179175
180176 @ Override
@@ -183,14 +179,14 @@ public int hashCode() {
183179 }
184180 }
185181
182+
186183 /**
187184 * CGLIB callback for filtering method interception behavior.
188185 */
189186 private static class MethodOverrideCallbackFilter extends CglibIdentitySupport implements CallbackFilter {
190187
191188 private static final Log logger = LogFactory .getLog (MethodOverrideCallbackFilter .class );
192189
193-
194190 MethodOverrideCallbackFilter (RootBeanDefinition beanDefinition ) {
195191 super (beanDefinition );
196192 }
@@ -210,11 +206,12 @@ else if (methodOverride instanceof LookupOverride) {
210206 else if (methodOverride instanceof ReplaceOverride ) {
211207 return METHOD_REPLACER ;
212208 }
213- throw new UnsupportedOperationException ("Unexpected MethodOverride subclass: "
214- + methodOverride .getClass ().getName ());
209+ throw new UnsupportedOperationException ("Unexpected MethodOverride subclass: " +
210+ methodOverride .getClass ().getName ());
215211 }
216212 }
217213
214+
218215 /**
219216 * CGLIB MethodInterceptor to override methods, replacing them with an
220217 * implementation that returns a bean looked up in the container.
@@ -223,7 +220,6 @@ private static class LookupOverrideMethodInterceptor extends CglibIdentitySuppor
223220
224221 private final BeanFactory owner ;
225222
226-
227223 LookupOverrideMethodInterceptor (RootBeanDefinition beanDefinition , BeanFactory owner ) {
228224 super (beanDefinition );
229225 this .owner = owner ;
@@ -233,10 +229,17 @@ private static class LookupOverrideMethodInterceptor extends CglibIdentitySuppor
233229 public Object intercept (Object obj , Method method , Object [] args , MethodProxy mp ) throws Throwable {
234230 // Cast is safe, as CallbackFilter filters are used selectively.
235231 LookupOverride lo = (LookupOverride ) getBeanDefinition ().getMethodOverrides ().getOverride (method );
236- return this .owner .getBean (lo .getBeanName ());
232+ Object [] argsToUse = (args .length > 0 ? args : null ); // if no-arg, don't insist on args at all
233+ if (StringUtils .hasText (lo .getBeanName ())) {
234+ return this .owner .getBean (lo .getBeanName (), argsToUse );
235+ }
236+ else {
237+ return this .owner .getBean (method .getReturnType (), argsToUse );
238+ }
237239 }
238240 }
239241
242+
240243 /**
241244 * CGLIB MethodInterceptor to override methods, replacing them with a call
242245 * to a generic MethodReplacer.
@@ -245,7 +248,6 @@ private static class ReplaceOverrideMethodInterceptor extends CglibIdentitySuppo
245248
246249 private final BeanFactory owner ;
247250
248-
249251 ReplaceOverrideMethodInterceptor (RootBeanDefinition beanDefinition , BeanFactory owner ) {
250252 super (beanDefinition );
251253 this .owner = owner ;
@@ -255,7 +257,7 @@ private static class ReplaceOverrideMethodInterceptor extends CglibIdentitySuppo
255257 public Object intercept (Object obj , Method method , Object [] args , MethodProxy mp ) throws Throwable {
256258 ReplaceOverride ro = (ReplaceOverride ) getBeanDefinition ().getMethodOverrides ().getOverride (method );
257259 // TODO could cache if a singleton for minor performance optimization
258- MethodReplacer mr = owner .getBean (ro .getMethodReplacerBeanName (), MethodReplacer .class );
260+ MethodReplacer mr = this . owner .getBean (ro .getMethodReplacerBeanName (), MethodReplacer .class );
259261 return mr .reimplement (obj , method , args );
260262 }
261263 }
0 commit comments