Skip to content

Commit a31ebb6

Browse files
committed
Polishing
1 parent 42c090e commit a31ebb6

File tree

10 files changed

+56
-90
lines changed

10 files changed

+56
-90
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -170,8 +170,7 @@ public interface BeanFactory {
170170
* <p>Allows for specifying explicit constructor arguments / factory method arguments,
171171
* overriding the specified default arguments (if any) in the bean definition.
172172
* @param name the name of the bean to retrieve
173-
* @param args arguments to use if creating a prototype using explicit arguments to a
174-
* static factory method. It is invalid to use a non-null args value in any other case.
173+
* @param args arguments to use if creating a prototype using explicit arguments
175174
* @return an instance of the bean
176175
* @throws NoSuchBeanDefinitionException if there is no such bean definition
177176
* @throws BeanDefinitionStoreException if arguments have been given but

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, Strin
239239
AnnotationAttributes annotation = findAutowiredAnnotation(candidate);
240240
if (annotation != null) {
241241
if (requiredConstructor != null) {
242-
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
242+
throw new BeanCreationException(beanName,
243+
"Invalid autowire-marked constructor: " + candidate +
243244
". Found another constructor with 'required' Autowired annotation: " +
244245
requiredConstructor);
245246
}
@@ -250,10 +251,10 @@ public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, Strin
250251
boolean required = determineRequiredStatus(annotation);
251252
if (required) {
252253
if (!candidates.isEmpty()) {
253-
throw new BeanCreationException(
254+
throw new BeanCreationException(beanName,
254255
"Invalid autowire-marked constructors: " + candidates +
255256
". Found another constructor with 'required' Autowired annotation: " +
256-
requiredConstructor);
257+
candidate);
257258
}
258259
requiredConstructor = candidate;
259260
}

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ protected <T> T doGetBean(
285285
if (dependsOn != null) {
286286
for (String dependsOnBean : dependsOn) {
287287
if (isDependent(beanName, dependsOnBean)) {
288-
throw new BeanCreationException("Circular depends-on relationship between '" +
289-
beanName + "' and '" + dependsOnBean + "'");
288+
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
289+
"Circular depends-on relationship between '" + beanName + "' and '" + dependsOnBean + "'");
290290
}
291291
registerDependentBean(dependsOnBean, beanName);
292292
getBean(dependsOnBean);
@@ -1274,7 +1274,7 @@ protected void checkMergedBeanDefinition(RootBeanDefinition mbd, String beanName
12741274
// Check validity of the usage of the args parameter. This can
12751275
// only be used for prototypes constructed via a factory method.
12761276
if (args != null && !mbd.isPrototype()) {
1277-
throw new BeanDefinitionStoreException(
1277+
throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName,
12781278
"Can only specify arguments for the getBean method when referring to a prototype bean definition");
12791279
}
12801280
}
@@ -1625,8 +1625,7 @@ protected void registerDisposableBeanIfNecessary(String beanName, Object bean, R
16251625
* instantiation within this class is performed by this method.
16261626
* @param beanName the name of the bean
16271627
* @param mbd the merged bean definition for the bean
1628-
* @param args arguments to use if creating a prototype using explicit arguments to a
1629-
* static factory method. This parameter must be {@code null} except in this case.
1628+
* @param args arguments to use if creating a prototype using explicit arguments
16301629
* @return a new instance of the bean
16311630
* @throws BeanCreationException if the bean could not be created
16321631
*/

spring-beans/src/main/java/org/springframework/beans/factory/support/CglibSubclassingInstantiationStrategy.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.springframework.beans.BeanInstantiationException;
2626
import org.springframework.beans.BeanUtils;
2727
import org.springframework.beans.factory.BeanFactory;
28-
2928
import org.springframework.cglib.core.SpringNamingPolicy;
3029
import org.springframework.cglib.proxy.Callback;
3130
import org.springframework.cglib.proxy.CallbackFilter;
@@ -89,14 +88,13 @@ protected Object instantiateWithMethodInjection(RootBeanDefinition beanDefinitio
8988
*/
9089
private static class CglibSubclassCreator {
9190

92-
private static final Class<?>[] CALLBACK_TYPES = new Class<?>[] { NoOp.class,
93-
LookupOverrideMethodInterceptor.class, ReplaceOverrideMethodInterceptor.class };
91+
private static final Class<?>[] CALLBACK_TYPES = new Class<?>[]
92+
{NoOp.class, LookupOverrideMethodInterceptor.class, ReplaceOverrideMethodInterceptor.class};
9493

9594
private final RootBeanDefinition beanDefinition;
9695

9796
private final BeanFactory owner;
9897

99-
10098
CglibSubclassCreator(RootBeanDefinition beanDefinition, BeanFactory owner) {
10199
this.beanDefinition = beanDefinition;
102100
this.owner = owner;
@@ -113,7 +111,6 @@ private static class CglibSubclassCreator {
113111
*/
114112
Object instantiate(Constructor<?> ctor, Object[] args) {
115113
Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
116-
117114
Object instance;
118115
if (ctor == null) {
119116
instance = BeanUtils.instantiate(subclass);
@@ -123,19 +120,17 @@ Object instantiate(Constructor<?> ctor, Object[] args) {
123120
Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());
124121
instance = enhancedSubclassConstructor.newInstance(args);
125122
}
126-
catch (Exception e) {
123+
catch (Exception ex) {
127124
throw new BeanInstantiationException(this.beanDefinition.getBeanClass(), String.format(
128-
"Failed to invoke construcor for CGLIB enhanced subclass [%s]", subclass.getName()), e);
125+
"Failed to invoke constructor for CGLIB enhanced subclass [%s]", subclass.getName()), ex);
129126
}
130127
}
131-
132128
// SPR-10785: set callbacks directly on the instance instead of in the
133129
// enhanced class (via the Enhancer) in order to avoid memory leaks.
134130
Factory factory = (Factory) instance;
135-
factory.setCallbacks(new Callback[] { NoOp.INSTANCE,//
136-
new LookupOverrideMethodInterceptor(beanDefinition, owner),//
137-
new ReplaceOverrideMethodInterceptor(beanDefinition, owner) });
138-
131+
factory.setCallbacks(new Callback[] {NoOp.INSTANCE,
132+
new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
133+
new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});
139134
return instance;
140135
}
141136

@@ -153,6 +148,7 @@ private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
153148
}
154149
}
155150

151+
156152
/**
157153
* Class providing hashCode and equals methods required by CGLIB to
158154
* ensure that CGLIB doesn't generate a distinct class per bean.
@@ -162,7 +158,6 @@ private static class CglibIdentitySupport {
162158

163159
private final RootBeanDefinition beanDefinition;
164160

165-
166161
CglibIdentitySupport(RootBeanDefinition beanDefinition) {
167162
this.beanDefinition = beanDefinition;
168163
}
@@ -173,8 +168,8 @@ RootBeanDefinition getBeanDefinition() {
173168

174169
@Override
175170
public boolean equals(Object other) {
176-
return other.getClass().equals(this.getClass())
177-
&& ((CglibIdentitySupport) other).getBeanDefinition().equals(this.getBeanDefinition());
171+
return (getClass().equals(other.getClass()) &&
172+
this.beanDefinition.equals(((CglibIdentitySupport) other).beanDefinition));
178173
}
179174

180175
@Override
@@ -183,14 +178,14 @@ public int hashCode() {
183178
}
184179
}
185180

181+
186182
/**
187183
* CGLIB callback for filtering method interception behavior.
188184
*/
189185
private static class MethodOverrideCallbackFilter extends CglibIdentitySupport implements CallbackFilter {
190186

191187
private static final Log logger = LogFactory.getLog(MethodOverrideCallbackFilter.class);
192188

193-
194189
MethodOverrideCallbackFilter(RootBeanDefinition beanDefinition) {
195190
super(beanDefinition);
196191
}
@@ -210,11 +205,12 @@ else if (methodOverride instanceof LookupOverride) {
210205
else if (methodOverride instanceof ReplaceOverride) {
211206
return METHOD_REPLACER;
212207
}
213-
throw new UnsupportedOperationException("Unexpected MethodOverride subclass: "
214-
+ methodOverride.getClass().getName());
208+
throw new UnsupportedOperationException("Unexpected MethodOverride subclass: " +
209+
methodOverride.getClass().getName());
215210
}
216211
}
217212

213+
218214
/**
219215
* CGLIB MethodInterceptor to override methods, replacing them with an
220216
* implementation that returns a bean looked up in the container.
@@ -223,7 +219,6 @@ private static class LookupOverrideMethodInterceptor extends CglibIdentitySuppor
223219

224220
private final BeanFactory owner;
225221

226-
227222
LookupOverrideMethodInterceptor(RootBeanDefinition beanDefinition, BeanFactory owner) {
228223
super(beanDefinition);
229224
this.owner = owner;
@@ -245,7 +240,6 @@ private static class ReplaceOverrideMethodInterceptor extends CglibIdentitySuppo
245240

246241
private final BeanFactory owner;
247242

248-
249243
ReplaceOverrideMethodInterceptor(RootBeanDefinition beanDefinition, BeanFactory owner) {
250244
super(beanDefinition);
251245
this.owner = owner;
@@ -255,7 +249,7 @@ private static class ReplaceOverrideMethodInterceptor extends CglibIdentitySuppo
255249
public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
256250
ReplaceOverride ro = (ReplaceOverride) getBeanDefinition().getMethodOverrides().getOverride(method);
257251
// TODO could cache if a singleton for minor performance optimization
258-
MethodReplacer mr = owner.getBean(ro.getMethodReplacerBeanName(), MethodReplacer.class);
252+
MethodReplacer mr = this.owner.getBean(ro.getMethodReplacerBeanName(), MethodReplacer.class);
259253
return mr.reimplement(obj, method, args);
260254
}
261255
}

spring-beans/src/main/java/org/springframework/beans/factory/support/LookupOverride.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public class LookupOverride extends MethodOverride {
3737

3838
/**
3939
* Construct a new LookupOverride.
40-
* @param methodName the name of the method to override.
41-
* This method must have no arguments.
40+
* @param methodName the name of the method to override
4241
* @param beanName the name of the bean in the current BeanFactory
4342
* that the overridden method should return
4443
*/
@@ -48,6 +47,7 @@ public LookupOverride(String methodName, String beanName) {
4847
this.beanName = beanName;
4948
}
5049

50+
5151
/**
5252
* Return the name of the bean that should be returned by this method.
5353
*/
@@ -63,10 +63,6 @@ public boolean matches(Method method) {
6363
return (method.getName().equals(getMethodName()) && method.getParameterTypes().length == 0);
6464
}
6565

66-
@Override
67-
public String toString() {
68-
return "LookupOverride for method '" + getMethodName() + "'; will return bean '" + this.beanName + "'";
69-
}
7066

7167
@Override
7268
public boolean equals(Object other) {
@@ -79,4 +75,9 @@ public int hashCode() {
7975
return (29 * super.hashCode() + ObjectUtils.nullSafeHashCode(this.beanName));
8076
}
8177

78+
@Override
79+
public String toString() {
80+
return "LookupOverride for method '" + getMethodName() + "'";
81+
}
82+
8283
}

spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverride.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected MethodOverride(String methodName) {
5252
this.methodName = methodName;
5353
}
5454

55+
5556
/**
5657
* Return the name of the method to be overridden.
5758
*/
@@ -99,6 +100,7 @@ public Object getSource() {
99100
*/
100101
public abstract boolean matches(Method method);
101102

103+
102104
@Override
103105
public boolean equals(Object other) {
104106
if (this == other) {

spring-beans/src/main/java/org/springframework/beans/factory/support/ReplaceOverride.java

Lines changed: 7 additions & 7 deletions
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-2014 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.
@@ -52,6 +52,7 @@ public ReplaceOverride(String methodName, String methodReplacerBeanName) {
5252
this.methodReplacerBeanName = methodReplacerBeanName;
5353
}
5454

55+
5556
/**
5657
* Return the name of the bean implementing MethodReplacer.
5758
*/
@@ -97,12 +98,6 @@ public boolean matches(Method method) {
9798
}
9899

99100

100-
@Override
101-
public String toString() {
102-
return "Replace override for method '" + getMethodName() + "; will call bean '" +
103-
this.methodReplacerBeanName + "'";
104-
}
105-
106101
@Override
107102
public boolean equals(Object other) {
108103
if (!(other instanceof ReplaceOverride) || !super.equals(other)) {
@@ -121,4 +116,9 @@ public int hashCode() {
121116
return hashCode;
122117
}
123118

119+
@Override
120+
public String toString() {
121+
return "Replace override for method '" + getMethodName() + "'";
122+
}
123+
124124
}

0 commit comments

Comments
 (0)